1

I'm trying to find a way to get all the names from the people list, without have to specify the path. Is there a way to do this in JMESPath. i.e without having to step down multiple paths.
I.e.: I want to grab a list of all the names.

{
  "country": {
    "people": [
      {
        "age": 20,
        "name": "Bob"
      },
      {
        "age": 25,
        "name": "Fred"
      }
    ]
  }
}

In the world of JSONata this would be:

**.name

# which would give you ...
[
  "Bob",
  "Fred"
]

Based on: jmespath how do I find the key values in the dictionary? it appears this cannot be done.

felix001
  • 15,341
  • 32
  • 94
  • 121

1 Answers1

1

In the world of JmesPath this is '*[].name'. For example, given the file

shell> cat people.json 
{
    "people": [
        {
            "age": 20,
            "name": "Bob"
        },
        {
            "age": 25,
            "name": "Fred"
        }
    ]
}
>>> import json
>>> import jmespath
>>> f = open('people.json')
>>> data = json.load(f)
>>> f.close()
>>> jmespath.search('*[].name', data)
['Bob', 'Fred']
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • Did you know that you do not need to go in such a burden to create a Python program in order to tests those? The JMESPath homepage is a playground for quarries, just replace the query and input in the respective boxes and the result will adapt. – β.εηοιτ.βε Oct 03 '22 at 08:00
  • 1
    [edit] your question. This is where you should provide [mre] what you're looking for. See [How do comments work?](https://meta.stackexchange.com/questions/19756/how-do-comments-work) and the section `What are comments for, and when shouldn't I comment?` in particular. Your comment doesn't require clarification, constructive criticism, or minor information. You should delete it. – Vladimir Botka Oct 03 '22 at 14:06