gjson icon indicating copy to clipboard operation
gjson copied to clipboard

How do I retrieve all values when nested dictionaries are present?

Open dnj12345 opened this issue 1 month ago • 2 comments

Hi, I am using https://gjson.dev/ to test the gjson library.

For the json shown below, I was providing the path as Nicknames.*.last. I get the result Andy Trevor. I was expecting it to return an array of values like [ "Andy Trevor", "Roger Binny" ]. When the target is an array, I can write the expression like thisfriends.#.last which returns ["Murphy","Craig","Murphy"].

The documentation describes # to refer to all elements of an array, but doesn't mention anything for dictionary of dictionary. I used * but that doesn't do I exected. Is what I am trying to do possible? Thanks.

Dk.

{
  "name": {"first": "Tom", "last": "Anderson"},
  "age":37,
  "children": ["Sara","Alex","Jack"],
  "fav.movie": "Deer Hunter",
  "Nicknames":{
     "home":{
        "last":"Andy Trevor"
      },
      "work":{
         "last":"Roger Binny"
      }
  }, 
  "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"]}
  ]
}

dnj12345 avatar Nov 05 '25 04:11 dnj12345

In this case you can use the @values modifier that returns the array of values of a dictionary and then get the last item for each of them. With:

Nicknames.@values.#.last

you get:

["Andy Trevor","Roger Binny"]

volans- avatar Nov 05 '25 07:11 volans-

Nice! That's neat. Thanks!

dnj12345 avatar Nov 05 '25 10:11 dnj12345