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

Filters not implemented #8

Open
aguynamedben opened this issue Nov 7, 2017 · 12 comments
Open

Filters not implemented #8

aguynamedben opened this issue Nov 7, 2017 · 12 comments

Comments

@aguynamedben
Copy link

aguynamedben commented Nov 7, 2017

Hi, thanks for your work on making a standards compliant jsonpath library for Python.

I think this library (and python-jsonpath-rw) might have a bug or are missing this part of the spec:

using the symbol '@' for the current object. Filter expressions are supported via the syntax ?() as in

$.store.book[?(@.price < 10)].title

...

$..book[?(@.isbn)] | filter all books with isbn number
$..book[?(@.price<10)] | filter all books cheapier than 10

I'm trying to filter an array by a string. My query, `$.phoneNumbers[?(@.type=='home')].number, works fine on the jsonpath.com testing tool, but I can't get it working using jsonpath-ng. It fails to parse the query, complaining about the question mark.

Example code:

import json
from jsonpath_ng import jsonpath, parse

data = '''
{
  "firstName": "John",
  "lastName" : "doe",
  "age"      : 26,
  "address"  : {
    "streetAddress": "naist street",
    "city"         : "Nara",
    "postalCode"   : "630-0192"
  },
  "phoneNumbers": [
    {
      "type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8910"
    }
  ]
}
'''
data = json.loads(data)
jsonpath_expr = parse(data)
jsonpath_query = "$.phoneNumbers[?(@.type=='home')].number"

Exception: JsonPathLexerError: Error on line 1, col 15: Unexpected character: ?

I'm trying this with jsonpath-ng>=1.4.3

Any ideas? Am I doing something wrong?

@ghost
Copy link

ghost commented Dec 5, 2017

change your import statements to use parse from the extensions

import json
from jsonpath_ng import jsonpath
from jsonpath_ng.ext import parse

@hubertgrzeskowiak
Copy link

Is this documented somewhere other than here?

@Pierric82
Copy link

After pulling my hair for an hour, I found this. NOW jsonpath works.

@AndydeCleyre
Copy link

Is there a solution for this when using the CLI?

@AndydeCleyre
Copy link

I tried changing the import in jsonpath_ng/bin/jsonpath.py, but am unable to figure out how to achieve via the CLI what passes at http://jsonpath.com/ as $.[?(@.name=="setuptools")].name,version, with the following json:

[
  {
    "name": "click",
    "version": "7.1.1"
  },
  {
    "name": "decorator",
    "version": "4.4.2"
  },
  {
    "name": "jsonpath-ng",
    "version": "1.5.1"
  },
  {
    "name": "pip",
    "version": "20.1.1"
  },
  {
    "name": "pip-tools",
    "version": "5.2.1"
  },
  {
    "name": "ply",
    "version": "3.11"
  },
  {
    "name": "setuptools",
    "version": "41.2.0"
  },
  {
    "name": "six",
    "version": "1.15.0"
  }
]

@alexander-jacob
Copy link

change your import statements to use parse from the extensions

import json
from jsonpath_ng import jsonpath
from jsonpath_ng.ext import parse

That would be a good annotation in the documentation ;-)

@HMcCloud
Copy link

@alexander-jacob 👍👍👍 Thanks a lot. when importing from jsonpath_ng import parse filter expressions fail with jsonpath_ng.lexer.JsonPathLexerError: .... Unexpected character: ? => importing from jsonpath_ng.ext import parse did the job.

@visionir-io
Copy link

I still have the parsing problem
fail
:(

@denven
Copy link

denven commented Mar 22, 2022

still not working today.
And the documentation is not good to digest.

@qwerty12
Copy link

qwerty12 commented Nov 12, 2022

Even with the import change, you still can't do $..[?(@ (note the ..), which jsonpath.com does allow. The only jsonpath library I found that lets you recursively search with a filter is https://github.com/hitplum/pyjsonpath, although I ended up using ObjectPath with its slightly different syntax. I have no affiliation with the authors of anything I've mentioned here.

@ludvikjerabek
Copy link

I'm laughing so hard right now. This is where all the great minds have converged only to realize it's the library, and not their code. For me, it would have been quicker to not use this JSONPath implementation due to the time I've wasted. So much for being compliant.

@NebularNerd
Copy link

NebularNerd commented Jul 31, 2023

Mmmmm this would explain a lot, I wondered why my .. search that worked in online testers was failing.

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

No branches or pull requests