How do I retrieve all values when nested dictionaries are present?
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"]}
]
}
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"]
Nice! That's neat. Thanks!