Json.NET SelectTokens doesn't work if json contains an empty array

I have a valid JSON object that contains multiple "en-US" keys that I am trying to select. I use JsonPath for this

"$..en-US"

      

which is exposed to the SelectTokens procedure implemented by Json.NET. It is a JSON framework for .NET. Everything works fine and fine as long as my JSON doesn't contain an 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 only contain one element! This will originate "en-US" before an empty array in the "controls" of the "header" object. However, when I just leave this "header" object:

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

      

I will receive all 3 "en-US" events as expected. Btw, if I check my JsonPath on the first JSON object (ie that contains an empty array) in the online tool , then as expected I get all three "en-US" cases. This is at odds with what I get from Json.NET. I am wondering if this is a bug or do I need to somehow handle this case?

+3


source to share


2 answers


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



+1


source


If you are in the same situation as me, where you are a bit stuck updating your version of Json.NET, you can work around this problem by doing something like this:

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

      



Hope it helps! The vals array will contain the same tokens that you would get with the selector you tried to use earlier.

0


source







All Articles