tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.1k stars 846 forks source link

How to query for an element from the root document #241

Closed bkielbasa closed 2 years ago

bkielbasa commented 2 years ago

Let's consider this example:

{
  "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"]}
  ]
}

I want to check if there's an age with the value of 37. I've tried things like this but none worked.

etc. How can I do it using jsons?

tidwall commented 2 years ago

Queries are for arrays. But you can "wrap" the root document in an array and then do a query like:

[@this].#(age==37)
bkielbasa commented 2 years ago

ok, it looks like it works! Thank