I have a JSON
file like this:
{
"objects": [{
"type": "FirstType",
(...)
"details": {
"id": 1,
"name": "FirstElementOfTheFirstType",
"font": "18px arial"
},
"id": "18e"
},
(...)
{
"type": "SecondType",
(...)
"details": {
"id": 1,
"name": "FirstElementOfTheSecondType",
"font": "18px arial"
},
"id": "18f"
}
],
"background": "#ffffff"
}
My goal is to delete nodes of a certain type
and id
in details
F.e. if I would like to delete elements of type
named FirstType
and id
of 1
.I would get:
{
"objects": [
(...)
{
"type": "SecondType",
(...)
"details": {
"id": 1,
"name": "FirstElementOfTheSecondType",
"font": "18px arial"
},
"id": "18f"
}
],
"background": "#ffffff"
}
I think I partialy achieved this:
final DocumentContext jsonContext = JsonPath.parse(element.getJsonContent());
jsonContext.delete("$['objects'][?(@.type == 'FirstType')][?(@.details.id == '1')]");
But I would like to consider type
and id
in details
as well, but I am not sure if two filter expressions are written correctly. I feel like I stuck here
EDITED: Solved
Ok. For future references, correct form goes like this:
DocumentContext jsonContext = JsonPath.parse(element.getJsonContent());
jsonContext.delete("$['objects'][?(@.type == 'FirstType' && @.details.id == 1)]");
element.setJsonContent(jsonContext.jsonString());