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

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

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.

Harikrish
New Contributor

Hi kmiesse,

Was an solution provided to this? I am still facing the issue while trying to filter a key whose value is null

acesario
Contributor II

Thank you tstack for the answer to a problem I was facing. I was able to remove nulls and dashes across a document using this a mapper: $.[?(value == โ€˜-โ€™ || value == null)]

My inbound document looked like this:

[
{
โ€œTRANSACTIONโ€: โ€œ32โ€,
โ€œDATE_OF_BIRTHโ€: โ€œ1995-09-21โ€,
โ€œSSNโ€: null,
โ€œBOX_OR_RESIDENCEโ€: โ€œ-โ€,
โ€œPRONOUN_DESCโ€: โ€œ-โ€,
โ€œAUDIT_ONLYโ€: โ€œNโ€,
โ€œHOMEโ€: โ€œYโ€,
โ€œHOME_SUPPRESSIONโ€: โ€œtbdโ€
}
]