simple and fast json encode/decode lib.
tree,err := Decode([]byte(`{"a":1}`)) // or tree := JSONTree{} err := json.Unmarshal([]byte(`{"a":1}`),&tree)
data, err := json.Marshal(tree)
A QJSON Path is a text string syntax that describes a search pattern for quickly retreiving values from a JSON payload.
A QJSON Path is intended to be easily expressed as a series of components seperated by a `.` character.
Given this JSON
{ "name": {"first": "Tom", "last": "Anderson"}, "age":37, "children": ["Sara","Alex","Jack"], "fav.movie": "Deer Hunter", "friends": [ {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} ] }
The following QJSON Paths evaluate to the accompanying values.
In many cases you’ll just want to retreive values by object name or array index.
name.last "Anderson" // tree.Find(`name.last`).AsString()
name.first "Tom" // tree.Find(`name.first`).AsString()
age 37 // tree.Find(`age`).AsInt()
children ["Sara","Alex","Jack"] // tree.Find(`children`).AsJSON()
children.0 "Sara" // tree.Find(`children.0`).AsString()
children.1 "Alex" // tree.Find(`children.1`).AsString()
friends.1 {"first": "Roger", "last": "Craig", "age": 68} // tree.Find(`friends.1`).AsJSON()
friends.1.first "Roger" // tree.Find(`friends.1.first`).AsString()
friends.#(nets.#(=="fb")).first ["Dale","Roger","Jane"] // == means equal
friends.#(nets.#(="f")).first ["Dale","Roger"] // = means contains
friends.#(last=Craig).first ["Roger"]
friends.#(age>47).first ["Roger"]
friends.#(age>=47).first ["Roger","Jane"]
friends.#(age<47).first ["Dale"]
friends.#(age<=47).first ["Dale","Jane"]
// example json ["a","b","c"]
#(!=c) ["a","b"] // not contains c
#(!==c) ["a","b"] // not equal c
Special purpose characters, such as ., can be escaped with .
fav\.movie "Deer Hunter"
You’ll also need to make sure that the `` character is correctly escaped when hardcoding a path in you source code.
// Go
tree.Find("fav\\.movie").AsString() // "Deer Hunter" must escape the slash
tree.Find(`fav\.movie`).AsString() // "Deer Hunter" no need to escape the slash
// rename Tom to Link
tree.Find(`name.first`).SetString("Link")
// change age to 12
tree.Find(`age`).SetInt(12)
goos: darwin goarch: amd64 pkg: github.com/qjpcpu/qjson cpu: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz BenchmarkMarshalQJSON-12 735711 1631 ns/op 1024 B/op 1 allocs/op BenchmarkUnmarshalQJSON-12 94591 12673 ns/op 1039 B/op 58 allocs/op BenchmarkMarshalStd-12 50536 23592 ns/op 9979 B/op 215 allocs/op BenchmarkUnmarshalStd-12 54302 21827 ns/op 9152 B/op 195 allocs/op PASS ok github.com/qjpcpu/qjson 8.084s