Closed petric3 closed 2 years ago
You can use gjson.GetMany with use r.Raw like:
results := gjson.Get(r.Raw, "str1", "value1")
Another option is to use gjson to iterate over the lines and array values like:
gjson.ForEachLine(json, func(line gjson.Result) bool {
line.ForEach(func(_, entry gjson.Result) bool {
res := gjson.GetMany(entry.Raw, "str1", "value1")
// some code using res[0] and res[1]
return true
})
return true
})
Thank you for the answer.
Interestingly, I benchmarked gjson.GetMany()
vs gjson.Get()
and gjson.Get()
is faster for about 15% although intiutively I'd say gjson.GetMany()
should be faster as gjson.Get()
has more calls than gjson.GetMany()
:
res := gjson.GetMany(r.Raw, "name1", "name2", "value1", "value2")
name1 = res[0].String()
name2 = res[1].String()
value1 = res[2].Float()
value2 = res[3].Float()
vs
name1 = r.Get("name1").String()
name2 = r.Get("name2").String()
value1 = r.Get("value1").Float()
value2 = r.Get("value2").Float()
Regarding the gjson.ForEachLine
, it's slower for my application than bufio
due to string conversion of a loaded file.
Thank you once more 🙏
You're welcome :)
I have a problem similar to this:
So far I've used it like this:
It would be awesome to have
res:=r.GetMany("str1","value1", etc)
forg.Array()
similar togjson.GetMany()
. I assume, this way it would be also faster than extracting each key viar.Get()
, t.i. going via a line only one time than nb of r.Get() times (?)Thanks a lot for the great library, it's super fast :)