I am trying to create a JSON filtering in one of my projects and here JSON will be dynamic so can not create a Model and filter will come from user, my sample JSON is
[
{
"id": 101,
"field1": "f1",
"field2": "f2",
"InnerArray": [
{
"id": 201,
"innerField1": "f1",
"innerField2": "f2"
},
{
"id": 202,
"innerField1": "f1",
"innerField2": "f2"
}
]
},
{
"id": 102,
"field1": "ff1",
"field2": "ff2",
"InnerArray": [
{
"id": 301,
"innerField1": "f1",
"innerField2": "f2"
},
{
"id": 302,
"innerField1": "f1",
"innerField2": "f2"
}
]
}
]
I am trying to filter this by SelectToken() and which will work fine except for inner array for example if the query is
string filter = "$.[?(@.id==101)]";
JToken filteredData = data.SelectToken($"{filter}");
//We will get
{
"id": 101,
"field1": "f1",
"field2": "f2",
"InnerArray": [
{
"id": 201,
"innerField1": "f1",
"innerField2": "f2"
},
{
"id": 202,
"innerField1": "f1",
"innerField2": "f2"
}
]
}
but if I want to filter the JSOn by inner array element then it will not work
string filter = "$.[?(@.InnerArray[?(@.id==301)])]";
JToken filteredData = data.SelectToken($"{filter}");
//Result is
{
"id": 102,
"field1": "ff1",
"field2": "ff2",
"InnerArray": [
{
"id": 301,
"innerField1": "f1",
"innerField2": "f2"
},
{
"id": 302,
"innerField1": "f1",
"innerField2": "f2"
}
]
}
My expectation is
{
"id": 102,
"field1": "ff1",
"field2": "ff2",
"InnerArray": [
{
"id": 301,
"innerField1": "f1",
"innerField2": "f2"
}
]
}
InnerArray Filter returning all elements and inner JSON PATH not taking, is there any alternative way to define JSON Path? or any alternative is there to dynamically filter JSON since here JSON will be dynamic and the filter will be dynamic