Tideland Go Dynamic JSON provides a simple dynamic handling of JSON documents. Values can be retrieved, set and added by paths like "foo/bar/3". Methods provide typesafe access to the values as well as flat and deep processing.
doc, err := dynaj.Unmarshal(myDoc)
if err != nil {
...
}
name := doc.ValueAt("/name").AsString("")
street := doc.ValueAt("/address/street").AsString("unknown")
Another way is to create an empty document with
doc := dynaj.NewDocument()
Here as well as in parsed documents values can be set with
err := doc.SetValueAt("/a/b/3/c", 4711)
Additionally values of the document can be processed recursively using
err := doc.Root().Process(func(node *dynaj.Node) error {
...
})
or from deeper nodes with doc.ValueAt("/a/b/3").Process(...)
.
Additionally flat processing is possible with
err := doc.ValueAt("/x/y/z").Range(func(node *dynaj.Node) error {
...
})
To retrieve the differences between two documents the function
dynaj.Compare()
can be used:
diff, err := dynaj.Compare(firstDoc, secondDoc)
privides a dynaj.Diff
instance which helps to compare individual
paths of the two document.
- Frank Mueller (https://github.com/themue / https://github.com/tideland / https://themue.dev)