GithubHelp home page GithubHelp logo

jsonhasher's Introduction

Consistent JSON Hasher in go

This module generates consistent json sha256 hashes irrespective of key orders.

Use cases:

  1. Compare two different JSONs for equality
  2. Generate keys based on JSON values in key value stores. (for example caching API response combination of inputs.)

Example Usage

package main

import (
    "fmt"
    "github.com/ranjith19/jsonhasher"
)

func main() {
    input1 := `{"a": 1, "c": {"x": true, "y": false}, "b": 2}`
    input2 := `{"b": 2, 
    "a": 1, "c": {"x": true, "y": false}}`

    h1, _ := jsonhasher.HashJsonString(input1)
    h2, _ := jsonhasher.HashJsonString(input2)

    fmt.Println(*h1)
    fmt.Println(*h2)
}

Will output

e8a8a051f649f3313bf6a02d3d117783ea53da1c6a7e7611b9d2a224496ed80b
e8a8a051f649f3313bf6a02d3d117783ea53da1c6a7e7611b9d2a224496ed80b

Irrespective of formatting on the JSON or the order of the keys, the hash will be consistent.

Try this at go playground. Another example here

Longer/Shorter hashes

You can use HashJsonStringSha1 or HashJsonStringSha512 methods.

Hashing structs

You can use HashInterface method to hash using exported fields of the struct. Example here

testing

Run tests by

go test

Run coverage

go test -covermode=count -coverprofile=coverage.out; go tool cover -html=coverage.out;

jsonhasher's People

Contributors

ranjith19 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.