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.

Thank you so much. It worked by creating an empty $array[*].subArray[]. Do you know where I can learn more about the syntax to use to manipulate JSON like this? I get stuck and spin my wheels a lot on these types of issues.

Also, is there a way to delete just a simple field, not an array/list, with the Mapper if its null like the above expression did for the array? When the right Target path is empty, I’m not sure what expression to use on the left to return the field when its null.

We have our own documentation for JSON-Path. There are also online testers, like:

Note that most JSON-Path implementations are only for reading data, whereas we use it for updating and deletions. So,

In the Designer, you should be able to drill down into the document structure in the Mapper and in the drop-down for expression properties.

It’s pretty much the same thing, except instead of checking for value.field1 == null you can check if value == null. So, you would write a path to the object whose fields you want to delete (e.g. $.foo.bar) and then use the filter operator ([?(... expr ...)]) to select the fields to delete:

$.foo.bar[?(value == null)]

If I have the following in a JSON Generator and a subsequent Mapper, what do I put in the Expression to remove “id” field if its null? I’m getting a parsing error when trying $.id[?(value == null)].
[
{
“id”: 21,
“eId”: [
256,
2,
548,
14010
]
},
{
“id”: null,
“eId”: [
256,
2,
548,
14010
]
}
]

Currently, in order to use the [?(...)] syntax in a JSON-Path, the previous part of the path needs to refer to an array or object. (Although I feel like your attempt makes sense and should have worked.)

To delete a property from an object, you need to filter the object and test for the property name and value, like so:

$[?(key == 'id' && value == null)]
1 Like

Thank you! That’s what I was hoping wasn’t the case. I opened a Support ticket for it and another issue I’m having filtering an array as documented.