Reputation: 187
I am mapping my payload to a new payload and adding an Errors array in where the output looks like this. :
payload : [{
"test: "test",
"test2" : "",
"test3" : "test3"
},
{
"test: "test",
"test2" : "test2",
"test3" : "test3"
}]
Expected output : ` payload : [{
"test: "test",
"test2" : "",
"test3" : "test3",
"Errors" : {
"test2" : "Test2 is NULL"
}
},
{
"test: "test",
"test2" : "test2",
"test3" : "test3",
"Errors" : {
}
}]`
Expected Output : I want to get an output where I only get all objects from Payload where Errors array has any key with not null values otherwise it should be filtered out.
I am using below expression to achieve but this is not feasible as it requires me to add a null check for each key in Errors array.
errArr."Errors" filter ((item, index) -> item."test" != "" or item."test2" != "" or
item."test3" != "")
There has to be a better way to do this? Is there a way to just check values of every item (key)without defining their name?
Upvotes: 0
Views: 3595
Reputation:
Try this, explanations follow the code:
%dw 2.0
output application/json
var data = [{
"test": "test",
"test2" : "",
"test3" : "test3"
},
{
"test": "test",
"test2" : "test2",
"test3" : "test3"
}
]
---
data map {
($),
errors: $ mapObject (v,k) -> (
if (isEmpty(v)) {(k): "$(k) is empty"} else {}
)
}
Here's the algorithm along with links in the documentation:
map
errors
field to the new object. Calculate the errors
object using the mapObject
function that identifies all empty (notice I say empty and not just null
) fields.My advice to you is to ensure you provide an appropriately scoped input and output set of sample data when you ask questions. This will ensure that you will get your answers to your questions in a more timely basis.
Upvotes: 2
Reputation: 25699
Note that there are a few errors in your input.
Script:
%dw 2.0
import * from dw::core::Arrays
import * from dw::core::Objects
output application/json
---
payload map {
($),
Errors: $
filterObject ((value, key) -> isEmpty(value))
mapObject ((value, key) -> (key): key ++ " is NULL")
}
Input:
[
{
"test": "test",
"test2" : "",
"test3" : "test3"
},
{
"test": "test",
"test2" : "test2",
"test3" : "test3"
}
]
Output:
[
{
"test": "test",
"test2": "",
"test3": "test3",
"Errors": {
"test2": "test2 is NULL"
}
},
{
"test": "test",
"test2": "test2",
"test3": "test3",
"Errors": {
}
}
]
Upvotes: 2
Reputation: 4303
Is this what you are after?
Input
[{
"test": "test",
"test2" : "test2",
"test3" : "test3",
"Errors" : {
"test": null,
"test2" : "Test2 is NULL",
"test3" : ""
}
},
{
"test": "1231test123",
"test2" : "123test23232",
"test3" : "12421test3",
"Errors" : {
"test": "",
"test2" : "",
"test3" : ""
}
},
{
"test": "3asdsadasd",
"test2" : "123123",
"test3" : "d323e2d23",
"Errors" : {
"test": "123",
"test2" : "",
"test3" : ""
}
}
]
Script
%dw 2.0
import * from dw::core::Arrays
import * from dw::core::Objects
output application/json
---
payload -- (payload map $ filter ( valuesOf( $.Errors ) some ( !isEmpty($) and ($ != null) and sizeOf($) >0)))
Output
[
{
"test": "1231test123",
"test2": "123test23232",
"test3": "12421test3",
"Errors": {
"test": "",
"test2": "",
"test3": ""
}
}
]
Upvotes: 1