I have the following JSON on which I want to get specific values using SelectToken -
{
"Result": {
"Id": "knownvalue0",
"Name": "Level0",
"Children": [
{
"Name": "Level1",
"Id": "knownvalue1",
"Children": [
{
"Name": "Level2",
"Id": "knownvalue2",
"Children": [
{
"Id": "knownvalue3",
"Name": "Level3",
"Children": []
}
]
}
]
}
]
}
}
The structure is recursive and I do not know the depth at runtime. However I do know the Ids and want to verify that the names are correct. So I use a JsonPath like this - Result..[?(@Id=='knownvalue')].Name in jtoken.parse(response).selecttoken(jsonpath) method. A problem is occurring when I am using this jsonpath on knownvalue1 or knownvalue2 i.e -
Result..[?(@Id=='knownvalue1')].Name
I am getting exception - Path is returning multiple tokens. Why is the selecttoken returning multiple tokens even though there is only 1 name with Id = 'knownvalue1'?? The Level2 and Level3 nodes are children, they should not be returned.
"Result..Children[?(@Id=='knownvalue1')].Name"works as expected, see dotnetfiddle.net/RvN0Zy. Not sure what's wrong with your query though.Result..[?(@Id=='knownvalue1')].NameandResult..Children[?(@Id=='knownvalue1')].Namegenerate an error -- butResult..[?(@.Id=='knownvalue1')].Name(andResult..Children[?(@.Id=='knownvalue1')].Name) work as expected when using Flow Communications JSONPath 0.4.0 -- but none work using Goessner.Result..*[?(@Id=='knownvalue1')].Namealso works if you don't want to hardcode the nameChildren. Apparently the recursive descent operator has to terminate in some sort of property name for the subsequent array indexer to behave as expected.