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

[Index expression] does not apply to array returned by [?filter expression] #9

Closed
gmaFFFFF opened this issue Oct 16, 2022 · 2 comments

Comments

@gmaFFFFF
Copy link

Hello,

$.books[[email protected]>22] will return an book array:

[
  {
    "category": "fiction",
    "title": "A Wild Sheep Chase",
    "author": "Haruki Murakami",
    "price": 22.72
  },
  {
    "category": "fiction",
    "title": "The Night Watch",
    "author": "Sergei Lukyanenko",
    "price": 23.58
  },
  {
    "category": "memoir",
    "title": "The Night Watch",
    "author": "David Atlee Phillips",
    "price": 260.90
  }
]

$.books[[email protected]>22][0] will retrun an empty array []
Many people think $.books[[email protected]>22][0] should return a book:

{
    "category": "fiction",
    "title": "A Wild Sheep Chase",
    "author": "Haruki Murakami",
    "price": 22.72
}

It looks like this problem affects all similar libraries - example

@danielaparker
Copy link
Owner

JSONPath basically collects values that are already in the original JSON document, it doesn't create new values, and as the last step, it appends all the values that it has collected into a result array. In JSONPath, the index operator can only be applied to items that are already arrays in the original JSON document, e.g., $.books[0]. The result array isn't created until all querying is done, it can't itself be queried against.

By contrast, there are other JSON query languages that do support the creation of new arrays as intermediate values, and these can be queried against. For example, the JMESPath expression

(books[?price > `8`])[0]

and the JSONAta expression

$.books[price > 8][0]

both produce

{
  "category": "fiction",
  "title": "A Wild Sheep Chase",
  "author": "Haruki Murakami",
  "price": 22.72
}

(JsonCons.Net has an implementation of JMESPath)

@gmaFFFFF
Copy link
Author

Thanks for the clarification

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

2 participants