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

JSONPath with bracket-notation and descendant axis returns more tokens than expected #2156

Closed
Viorel opened this issue Sep 10, 2019 · 1 comment · Fixed by #2180
Closed

Comments

@Viorel
Copy link

Viorel commented Sep 10, 2019

Source JSON

{
    "root": {
        "a": {
            "name": "John",
            "b": {
                "name": "Sarah"
            }
        }
    }
}

Expected behavior

Using SelectToken with JSONPath, I want to search for “a” recursively, then get the “name” non-recursively. I expect to find a single token: the “name” node, which is an immediate child of “a”.

Actual behavior

When I use the dot-notation — “$..a.name” — it returns correctly the “name” token, which is a child of “a”.

However, when I use the alternative bracket-notation — “$..['a']['name']” — it returns both of the “name” tokens. The second “name” should not be returned, because it is not an immediate child of “a”.

Steps to reproduce

// In C Sharp, in a console application,
// using Newtonsoft.Json 12.0.2 and .NET Framework 4.8.

string json = @"
        {
            ""root"": {
                ""a"": {
                    ""name"": ""John"",
                    ""b"": {
                        ""name"": ""Sarah""
                    }
                }
            }
        }                 
        ";

JToken t = JToken.Parse( json );

int count1 = t.SelectTokens( "$..a.name" ).Count(); // result: 1, expected: 1
int count2 = t.SelectTokens( "$..['a']['name']" ).Count(); // result: 2, expected: 1
@JamesNK
Copy link
Owner

JamesNK commented Sep 30, 2019

Thanks for the good repro. Fixed.

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

Successfully merging a pull request may close this issue.

2 participants