forked from kennknowles/python-jsonpath-rw
-
Notifications
You must be signed in to change notification settings - Fork 99
Open
Labels
Description
Hi, I'm sure that this is user error from me, but I'm stuck trying to perform the following:
JSON data is as shown below, and I am trying to return only the first record on the basis that I do not want to return records that have a key "type" defined.
[
{
"name": "foo",
"data": [
{
"value": "bar"
}
]
},
{
"name": "foo",
"data": [
{
"value": "bar",
"type": "foo"
}
]
}
]
I have tried with the following jsonpath_query = "$[?(@['name']=='foo'&@['data'][0]['value'] == 'bar'&@['data'][0]['type'])]"
as a basis, but I want to exclude the records rather than include them. e.g. not(@['data'][0]['type'])
Is this possible with jsonpath-ng or must I first match all the data records, and then filter out those records with a "type" field? e.g.
data = '''[
{ "name": "foo", "data": [ { "value": "bar", "other": "ham" } ] },
{ "name": "foo", "data": [ { "value": "bar", "type": "foo" } ] }
]
'''
data = json.loads(data)
q = "$[?(@['name']=='foo'&@['data'][0]['value'] == 'bar')]"
f = "$[?(@['data'][0]['type'])]"
expr = parse(q)
f_expr= parse(f)
for match in f_expr.filter(lambda d: True, expr.find(data)):
print(match.value)
Many thanks.