sierra_papa
sierra_papa

Reputation: 394

Extracting a nested array using JMESPath with reference to the outer object in the output

How can I use JMESPath in Python to extract the array [["S", "2313711"], ["S", "15366545"]] from the data below:

data = {
    "categories": [
        {
         "sport": "S",
         "children": [
           {"id": "2313711"},
           {"id": "15366545"}
          ]
        }
    ]
}

Please note that the length of the children array may vary.

So far, I tried:

jmespath.search("categories[][ [ sport, children[].id] ][]", data)
# output: [['S', ['2313711', '15366545']]]

This is not what I want, as it groups all child IDs under one list instead of generating separate lists for each child ID and the corresponding sport value.

Upvotes: 0

Views: 24

Answers (0)

Related Questions