kmiesse
8 years agoContributor
Delete Array or Create Empty Array Based on Expression
If I have the following,
[{
"fieldType": null,
"array": [{
"fieldA": null,
"subArray": [{
"field1": null
}]
}]
},
{
"fieldType": null,
"array": [{
"fieldA": null,
"subArray": [{
"field1": 1,
"field2": "asdf",
"field3": "fdff"
}]
}]
}]
how do I set the subArray with field1 = null to an empty array or completely remove subArray like the following:
– EMPTY ARRAY
[{ "fieldType": null, "array": [{ "fieldA": null, "subArray": [] }] }, { "fieldType": null, "array": [{ "fieldA": null, "subArray": [{ "field1": 1, "field2": "asdf", "field3": "fdff" }] }] }]
OR
– DELETE ARRAY
[{ "fieldType": null, "array": [{ "fieldA": null }] }, { "fieldType": null, "array": [{ "fieldA": null, "subArray": [{ "field1": 1, "field2": "asdf", "field3": "fdff" }] }] }]
You might want to look at this post that had a similar request.
In short, you can use a Mapper with a JSON-Path in the left field of a mapping row with an empty right field to delete all elements on the path. In this case, you’d probably use a path like the following:
$array[*].subArray[?(value.field1 == null)]
Make sure you have the ‘Passthrough’ option enabled in the Mapper.