Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to exclude a data set. #167

Open
andyjsharp opened this issue Mar 26, 2024 · 0 comments
Open

Unable to exclude a data set. #167

andyjsharp opened this issue Mar 26, 2024 · 0 comments

Comments

@andyjsharp
Copy link

andyjsharp commented Mar 26, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants