1

I am trying to use JSON.NET SelectToken with JSONPath to select for example the value of longname (eee) where types is only ggg.

JSON snippet:

"results" : [
  {
     "components" : [
        {
           "longname" : "aaa",
           "shortname" : "bbb",
           "types" : [ "ccc", "ddd" ]
        },
        {
           "longname" : "eee",
           "shortname" : "fff",
           "types" : [ "ggg" ]
        }
     ]
  }]

I am unable to get the JSONPath syntax correct. I have tried the following which I think is close to the right solution but always returns null:

 o.SelectToken("results[0].components[?(@.types=='[ggg]')].longname")

I have referenced Querying JSON with SelectToken and # JSONPath - XPath for JSON with no joy.

0

1 Answer 1

2

Ok, so I learnt a bit about JSONPath and had a play around (using this), and I am not sure if this is the best solution, but it definitely works:

results[0].components[?(@.types.length==1&&@.types[0]=="ggg")].longname

Unfortunately I cannot seem to find any other information to suggest there is a more simple solution to this.

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

1 Comment

thanks - it appears from here that @.length on arrays isn't supported.

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.