Skip to content
New issue

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

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

Already on GitHub? # to your account

Cannot support complex json path with parentheses #3012

Open
shuitai opened this issue Jan 22, 2025 · 2 comments
Open

Cannot support complex json path with parentheses #3012

shuitai opened this issue Jan 22, 2025 · 2 comments

Comments

@shuitai
Copy link

shuitai commented Jan 22, 2025

This is valid json path : $[?((@['MetricType'] == '2' && @['Field1']) || (@['MetricType'] == '3' && @['Field2']))]

I have validated it in https://jsonpath.com/

But newtonjson(13.0.1) parse error:

Newtonsoft.Json.JsonException: Unexpected character while parsing path query: (
at Newtonsoft.Json.Linq.JsonPath.JPath.ParseSide()
at Newtonsoft.Json.Linq.JsonPath.JPath.ParseExpression()
at Newtonsoft.Json.Linq.JsonPath.JPath.ParseQuery(Char indexerCloseChar, Boolean scan)
at Newtonsoft.Json.Linq.JsonPath.JPath.ParseIndexer(Char indexerOpenChar, Boolean scan)
at Newtonsoft.Json.Linq.JsonPath.JPath.ParsePath(List`1 filters, Int32 currentPartStartIndex, Boolean query)
at Newtonsoft.Json.Linq.JsonPath.JPath.ParseMain()
at Newtonsoft.Json.Linq.JsonPath.JPath..ctor(String expression)
at Newtonsoft.Json.Linq.JToken.SelectToken(String path, JsonSelectSettings settings)
at Newtonsoft.Json.Linq.JToken.SelectToken(String path)

@shuitai
Copy link
Author

shuitai commented Jan 22, 2025

Json Path RFC 9535

https://json-everything.net/json-path could support it

@elgonzo
Copy link

elgonzo commented Jan 22, 2025

JsonPath support in Newtonsoft.Json is limited.

Note that the JsonPath support in Newtonsoft.Json is based on the the very old Goessner spec (which the documentation links to here: https://www.newtonsoft.com/json/help/html/SelectToken.htm#SelectTokenJSONPath).

If you want/need more extensive and up-to-date JsonPath support that support expression like the one in your report, i suggest you migrate to System.Text.Json with the JsonPath.NET package (https://github.com/json-everything/json-everything, https://docs.json-everything.net/path/basics/). Plus, there is an online JsonPath evaluator powered by JsonPath.NET itself (https://json-everything.net/json-path/, which you already discovered yourself), so you could very easily test the exact feature set of JsonPath.NET.

If for whatever reason you must or prefer to stick with Newtonsoft.Json, well..., then you can't use JsonPath expressions like the one you mentioned. Rather, you would need to either find ways to simplify the expression so it adheres to the Goessner spec. Or, if the former is not possible, break the expression down into multiple simpler expressions and write appropriate "glue" code logic that yields the desired results. (Side note: Newtonsoft.Json does not adhere to the Goessner spec perfectly. For example, .* and [*] are supposed to be synonymous, but Newtonsoft.Json treats them differently.)

Also take a look at the development activities of the Newtonsoft.Json library during the last years, and you should be able to form your own opinion about how likely it is for Newtonsoft.Json to receive a major feature update like expanded JsonPath support...

(Disclaimer: I am not the author/maintainer of Newtonsoft.Json, nor am i involved in its maintenance. I am just a -mostly former- user of Newtonsoft.Json.)

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

No branches or pull requests

2 participants