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 #54

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

Filters not implemented #54

aguynamedben opened this issue Nov 7, 2017 · 14 comments

Comments

@aguynamedben
Copy link

aguynamedben commented Nov 7, 2017

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

I think this library 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_rw 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-rw>=1.4.0

Any ideas? Am I doing something wrong?

@foxel
Copy link

foxel commented Dec 8, 2017

+1

6 similar comments
@danbaehr
Copy link

danbaehr commented Dec 8, 2017

+1

@saravanan10393
Copy link

+1

@dmlb2000
Copy link

+1

@christian-bos
Copy link

+1

@mocobk
Copy link

mocobk commented Aug 12, 2018

+1

@SCORE1387
Copy link

+1

@chengshuangliu
Copy link

+10086

@kennknowles
Copy link
Owner

Contributions welcome! I will try to get existing pull requests resolved and find some more committers here, since I have no time to add features.

@danbaehr
Copy link

danbaehr commented Sep 29, 2018 via email

@dmlb2000
Copy link

and jsonpath2

@district10
Copy link

http://www.ultimate.com/phil/python/#jsonpath this one works

@hrobertson
Copy link

Just a note for those looking for negation on filtering.. eg. $[?([email protected])] you can find it in this PR: h2non#21. Note you need to import parse from jsonpath_ng.ext

@ernestomi
Copy link

ernestomi commented Jun 5, 2019

If you are looking for filtering then jsonpath2 or Phil's library as suggested by @district10. I just thought I should add a sample use for Phil's library as it took me a while to find it.

For everything else, I much prefer this project (functionality, robustness, documentation and community)

import jsonpath
#get specific value of a 
contents ={
              "id": 534253245,
              "sku": "23453245324",
              "name": "Sample",
              "attribute_set_id": 23453245,
              "price": 23453245,
              "status": 1,
              "visibility": 1,
              "created_at": "1989-07-19 08:43:36",
              "updated_at": "1989-07-19 08:43:36",
              "weight": 453,
              "custom_attributes": [
                {
                  "attribute_code": "meta_title",
                  "attribute_value": "Sample Meta Tile"
                },
                {
                  "attribute_code": "meta_keyword",
                  "attribute_value": "Sample"
                }
              ]
            }
jsonpath.jsonpath(contents,'$.custom_attributes[?(@.attribute_code=="meta_title")].attribute_value')
#Output:
#['Sample Meta Tile']

Equivalent for jsonpath2

from jsonpath2.path import Path
p = Path.parse_str('$.custom_attributes[*][?(@.attribute_code="meta_title")].attribute_value')
list(map(lambda match_data: match_data.current_value, p.match(contents)))

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