Search code examples
pythonjmespath

jmespath search nested array issue


I need to search all dict in a nested array as below by its key with jmespath

my_list = [[{'age': 1, 'name': 'kobe'}, {'age': 2, 'name': 'james'}], [{'age': 3, 'name': 'kobe'}]]

I got an empty list with jmespath search: jmespath.search("[][?name=='kobe']", my_list)

how can I get result: [{'age': 1, 'name': 'kobe'}, {'age': 3, 'name': 'kobe'}] with jmespath search


Solution

  • Use the following jmesQuery:

    []|[?name=='kobe']
    

    on input:

    [[{"age": 1, "name": "kobe"}, {"age": 2, "name": "james"}], [{"age": 3, "name": "kobe"}]]
    

    to get output:

    [
      {
        "age": 1,
        "name": "kobe"
      },
      {
        "age": 3,
        "name": "kobe"
      }
    ]