10

Here's my json:

{
   'test': [
        { "id": "1", "description": "Test 1" },
        { "id": "2", "description": "Test 2" }
    ]
}

I'm trying to get the value for id where description is "Test 1".

I found the following example on the JsonPath page:

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

When trying to parse the following jsonxpath expression:

parse('$..test[?(@.description="Test 1")].id')

I get the following error:

jsonpath_rw.lexer.JsonPathLexerError: Error on line 1, col 7: Unexpected character: ?

What am I doing wrong? Alternatively, is there a better way to do this?

1
  • 3
    Oddly enough, looking at the lexer code, jsonpath-rw doesn't appear to support the ?(boolean) filter mechanism at all. Commented May 24, 2015 at 1:13

1 Answer 1

10

It appears that jsonpath-rw doesn't support this feature. Perhaps consider another library? ObjectPath looks promising:

>>> import objectpath
>>> json_obj = { ... } # This has to be pre-parsed
>>> tree = objectpath.Tree(json_obj)
>>> ids = tree.execute('$..test[@.description is "Test 1"].id')
>>> print list(ids)
["1"]

It doesn't quite follow the JsonPath syntax exactly, but it's quite similar, at least from a perfunctory survey. Documentation is also available.

Of course, if your JSON will always be in the same form (i.e. you won't have to deal with missing child objects, etc), then you could easily do the same query using a list comprehension, or something similar.

json = { ... }
ids = [t['id'] for t in json['test'] if t['description'] == 'Test 1']
Sign up to request clarification or add additional context in comments.

1 Comment

Just keep in mind that .. operator (recursive descent/deep search) is very slow. Use . if you are sure that test is in the root of the document and only in the root of the document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.