This question is very related to the following one in
Querying JSON with JSONPath or SelectTokens? With JSON.NET in C#
Similar to the question above, how can I make Where
queries when the JSON object does not always have the same simplified data structure, i.e.
{
"video": {
"local_recording_device": {
"codecs": null
},
"preferred_string": "___PREFERRED___",
"streams": {
"22855218": {
"id": "22855218",
"name": "AJA Camera"
},
"99176901": {
"id": "99176901",
"name": "PTZ Camera",
"site": "someone",
"email": "someone@awebsite.com",
"codec": [
"VP8",
"HD1",
"(720p)"
]
},
"3091494011": {
"id": "3091494011",
"name": "Logitech Webcam C930e",
"site": "Joe Smith",
"email": "joe@awebsite.com",
"codec": [
"VP8",
"Medium",
"(CIF)"
]
},
"3798287599": {
"id": "3798287599",
"name": "Drive Camera",
"site": "ASiteName",
"email": "asitesame@awebsite.com",
"codec": [
"HD1",
"(720p)"
]
}
}
}
}
How can I get the Where
query to work on the given codec, e.g. "VP8"?
It doesn't seem to work with my applications if I query it like this:
string json = GetJson();
var obj = JObject.Parse(json);
var testcodec = "VP8";
var streamQuery = obj.SelectTokens("video.streams.*.codec")
.Where(s =>(string)s == testcodec);
But my result should look like:
{
"video":{
"local_recording_device":{
"codecs":null
},
"preferred_string":"___PREFERRED___",
"streams":{
"99176901":{
"id":"99176901",
"name":"PTZ Camera",
"site":"someone",
"email":"someone@awebsite.com",
"codec":[
"VP8"
]
},
"3091494011":{
"id":"3091494011",
"name":"Logitech Webcam C930e",
"site":"Joe Smith",
"email":"joe@awebsite.com",
"codec":[
"VP8"
]
}
}
}
}