3

I have a valid JSON object that contains multiple "en-US" keys, which I'm trying select. For that purpose I use the JsonPath

"$..en-US"

which is given to the SelectTokens procedure implemented by the Json.NET. It's a JSON framework for .NET . Everything is working fine and well as long as my JSON doesn't contain any empty array. Here's an example:

var myJsonPath = "$..en-US";
var myJson = 
        @"{
            'controls': [
                {
                    'messages': {
                        'addSuggestion': {
                            'en-US': 'Add'
                        }
                    }
                },
                {
                    'header': {
                        'controls': []
                    },
                    'controls': [
                        {
                            'controls': [
                                {
                                    'defaultCaption': {
                                        'en-US': 'Sort by'
                                    },
                                    'sortOptions': [
                                        {
                                            'label': {
                                                'en-US': 'Name'
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }";
var jToken = JObject.Parse(myJson);
var tokens = jToken.SelectTokens(myJsonPath);

Here, the tokens variable will contain just one element! That will be the "en-US" occurence BEFORE the empty array in the 'controls' of the 'header' object. However, when I just leave this 'header' object out:

var myJson = 
    @"{
        'controls': [
            {
                'messages': {
                    'addSuggestion': {
                        'en-US': 'Add'
                    }
                }
            },
            {
                'controls': [
                    {
                        'controls': [
                            {
                                'defaultCaption': {
                                    'en-US': 'Sort by'
                                },
                                'sortOptions': [
                                    {
                                        'label': {
                                            'en-US': 'Name'
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }";

I will get all the 3 occurencies of the "en-US" as expected. Btw, if I validate my JsonPath on the first JSON object (i.e. which contains an empty array) in an online tool, then as expected, I get all the three "en-US" cases. This diverges from what I'm getting from the Json.NET. I'm wondering whether it's a bug or do I have to handle this case manually somehow?

2 Answers 2

1

This is a bug that has been fixed. Upgrade to the latest version of Json.NET.

Sign up to request clarification or add additional context in comments.

Comments

0

If you're in the same situation as me where you're a bit stuck with respect to updating your version of Json.NET, you can work around the issue by doing something along the lines of this:

IEnumerable<JValue> vals = jToken
    .Desecendants()
    .Where(w => w is JProperty && w.Name=="en-US")
    .Select(s => s.Value);

Hope that helps! The vals array will contain the same tokens you would have gotten using the selector you were trying to use before.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.