tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.21k stars 847 forks source link

How to get length of array after running a query inside array of nested object #303

Closed sharadregoti closed 1 year ago

sharadregoti commented 1 year ago

With the below sample data set

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

If execute the query "children.#", It gives me the length of an array.

Now, If run this query friends.#(last=="Murphy")# It gives me the filtered result to access that is >> ["Dale","Jane"]. But not the length.

How can i get the length of last query result? friends.#(last=="Murphy")# >> 2

I tried this friends.#(last=="Murphy").# But this gave me empty result

volans- commented 1 year ago

@sharadregoti you can get it with the pipe | separator: friends.#(last=="Murphy")#|# >> 2 See also the Dot vs Pipe section of the syntax page for more details about it.

sharadregoti commented 1 year ago

Great it worked!!