Barry Kaye
Barry Kaye

Reputation: 7759

JSON.NET SelectToken with JSONPath

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.

Upvotes: 1

Views: 886

Answers (1)

musefan
musefan

Reputation: 48415

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.

Upvotes: 2

Related Questions