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

Valid JsonPath expression $.* on Json Array results in exception #2913

Open
gorillapower opened this issue Nov 20, 2023 · 1 comment
Open

Comments

@gorillapower
Copy link

gorillapower commented Nov 20, 2023

Source/destination JSON

[{"item":"two"},{"item":"two"},{"item":"two"}]

Expected behavior

Executing JToken.SelectTokens("$.*") on a valid JSON array should produce multiple JToken results

Actual Behavior

Newtonsoft.Json.JsonException: Property '*' not valid on JArray.
   at Newtonsoft.Json.Linq.JsonPath.FieldFilter.ExecuteFilter(JToken root, IEnumerable`1 current, JsonSelectSettings settings)+MoveNext()
   at System.Collections.Generic.LargeArrayBuilder…

Repro Steps

https://dotnetfiddle.net/ginhZu

  • Net 6.0.0 and 8.0.0
  • Newtonsoft.Json 13.0.3
var json = """[{"item":"two"},{"item":"two"},{"item":"two"}]""";
 
var parsed = JToken.Parse(json);
var query = parsed.SelectTokens("$.*",true).ToList();    

Running the same query here https://json-everything.net/json-path produces a valid result.

WorkAround

JsonPath $[*] products the expected result

@elgonzo
Copy link

elgonzo commented Nov 20, 2023

To add to the report, according to the JsonPath draft spec, section 1.4.4, .* is a shorthand/alias for [*]. Therefore, $.* is equivalent to $[*].

However, not only does Newtonsoft.Json implement .* incorrectly, it also implements [*] incorrectly.

Since .* is a shorthand/alias for [*], both .* and [*] should be applicable to objects AND arrays equally. But Newtonsoft.Json treats both .* and [*] differently, in violation of the JsonPath draft spec i linked to above. .* works on objects but incorrectly not on arrays, as the report pointed out. And [*] works on arrays but incorrectly not on objects.


P.S.: While the very first Json draft spec version 00 -- draft-ietf-jsonpath-base-00 -- also already explains the .* semantics in section 3.6.1, the draft spec version 00 seems to lack any formal definition for [*] -- it only mentions

JSONPath allows the wildcard symbol "*" for member names and array indices

in passing. The same mention appears at https://goessner.net/articles/JsonPath/, and Newtonsoft.Json therefore seems to also violate the old Goessner draft from 2007 in this respect...

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