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