Forum Discussion
There are a couple of options, maybe more.
I use sl.ensureArray quite frequently. It converts the object to an array, if it is not already an array. sl.ensureArray($myObjectOrArray)
Also available is typeof, which comes in handy in other cases. typeof $myObjectOrArray == "array"
Thanks for the same answer only better with Documentation links 🙂
The Mapper can delete fields based on a JSON-Path and you can do quite a bit with JSON-Path. If you wanted to delete fields whose values were null, you could add a Mapper, set “Pass through” to true, and add one row with this expression:
$…[?(value == null)]
Note that there is nothing in the ‘Target Path’ column for this row.
Here’s a breakdown of the JSON-Path:
$… - This tells the mapper to walk the entire JSON object hierarchy.
[?(value == null)] - This is a conditional that tells the mapper to visit only those fields whose value is null.- stephenknilans8 years agoContributor
Is there a way to get tims method to work on the entire reference?
We start with:
, {
“value”: {
“AddressType”: {
“value”: null
},
“AddressLine1”: {
“value”: null
},
“City”: {
“value”: null
},
“StateProvince”: {
“value”: null
},
“Zip”: {
“value”: null
},
“Country”: {
“value”: null
}
}And want nothing to appear.
Tim’s method gives us:
, {
“value”: {
“AddressType”: {},
“AddressLine1”: {},
“City”: {},
“StateProvince”: {},
“Zip”: {},
“Country”: {}
}If you wanted to delete objects where the ‘value’ field is null, you can use this path:
$..[?(value instanceof Object && value.value == null)]
The
..
part of the path means that the entire hierarchy should be traversed. The[?(...)]
portion is a filter that is applied to each object in the hierarchy. If the result of the expression in the filter is true, the object will be deleted, otherwise it is left alone. In this case, we check to see if the object we’re visiting (referenced with thevalue
variable) is an object and if it’svalue
field is null.If you wanted to delete fields with empty objects instead, you can tweak the expression to call the isEmpty() method, like so:
$..[?(value instanceof Object && value.isEmpty())]
Related Content
- 5 years ago
- 12 months ago
- 2 years ago
- 2 years ago
- 4 months ago