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

array.length jsonpath for the selectToken method returned null instead of count #2916

Open
Rahna-S opened this issue Dec 1, 2023 · 1 comment

Comments

@Rahna-S
Copy link

Rahna-S commented Dec 1, 2023

"arrayobject.length" jsonpath for the method selecttoken returned null instead of numerical value.

Expected behavior

It should return the array count.

Actual behavior

Returned null.

Steps to reproduce

// Your calls to Newtonsoft.Json here
string jsonExpressionText = "$.attachment.length";
JToken token = jsonObject.SelectToken(jsonExpressionText);

Here, attachment is an array and the expression returns null.
@Rahna-S Rahna-S changed the title .length jsonpath for an array is supported selecttoken method .length jsonpath for an array for the selectToken method returned null instead of count Dec 1, 2023
@Rahna-S Rahna-S changed the title .length jsonpath for an array for the selectToken method returned null instead of count array.length jsonpath for the selectToken method returned null instead of count Dec 1, 2023
@elgonzo
Copy link

elgonzo commented Dec 1, 2023

I am only a user of Newtonsoft.Json and not otherwise associated with the project or its author/maintainer. That said, i would strongly suggest you ponder a possible migration from Newtonsoft.Json to System.Text.Json if JsonPath support is important to you. While JsonPath support in System.Text.Json itself is lacking as of now, there is the (in my opinion) excellent JsonPath.Net library (doc, github), which not only is substantially better than Newtonsoft.Json's JsonPath support (striving to support the current JsonPath specification), but it also is actively maintained. Plus, there is a handy JsonPath.Net-based online evaluator for quickly trying out and messing about with JsonPath expressions (https://json-everything.net/json-path/). However, JsonPath.Net relies on STJ and can't be used on top of Newtonsoft.Json.

If you don't want or cannot migrate to STJ, then i guess you unfortunately will be stuck with Newtonsoft.Json's quirky and outdated JsonPath implementation. Support for the length function (and other functions in JsonPath) has been asked for years ago (see this open ticket, for example: #1318), and in my frank opinion i would see it as a small miracle if Newtonsoft.Json were to receive noteworthy feature updates anymore. (Personally, i consider Newtonsoft.Json a largely feature-frozen legacy library, with System.Text.Json being the go-to for Json processing in .NET these days.)

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