I’d like to select elements from a list based on the value of bar
in the params
key-value pair. See example below.
I have the following code:
with open('foobar.json', 'r') as fp:
data = json.load(fp)
expr = '[].record'
records = jmespath.search(expr, data)
And the following input file (foobar.json
)
[
{
"record": {
"name": "foobar1",
"params": [
{
"name": "foo",
"text": "4"
},
{
"name": "bar",
"text": "12"
}
]
}
},
{
"record": {
"name": "foobar2",
"params": [
{
"name": "foo",
"text": "4"
},
{
"name": "bar",
"text": "16"
}
]
}
}
]
The JMESPath query returns both records, as expected.
[ { 'name': 'foobar1',
'params': [ {'name': 'foo', 'text': '4'},
{'name': 'bar', 'text': '12'}]},
{ 'name': 'foobar2',
'params': [ {'name': 'foo', 'text': '4'},
{'name': 'bar', 'text': '16'}]}]
I’d like a JMESPath expression that would return only this one record because bar > 12
.
[ { 'name': 'foobar2',
'params': [ {'name': 'foo', 'text': '4'},
{'name': 'bar', 'text': '16'}]}]