cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Delete Array or Create Empty Array Based on Expression

kmiesse
Contributor

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"
		}]
	}]
}]
1 ACCEPTED SOLUTION

tstack
Former Employee

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.

View solution in original post

8 REPLIES 8

tstack
Former Employee

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.

tstack
Former Employee

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
]
}
]