0
votes

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?

3
Just for the record the empty string is not null in Dataweave. - George
yes, I understand that. my goal here is to either addd errors to the array only if the validation fails for a specific key and if that is not possible, I need to extract success row (no error in errors). - anxiousAvocado
Create an exprected output because your sentence: 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. makes little sense to me. Form what I can gather based upon your specific input you should have an empty array. - George
@George sorry about that, I was allover the place, I updated the input output, I hope it's a bit more clear now. - anxiousAvocado

3 Answers

2
votes

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": {
      
    }
  }
]
2
votes

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:

  1. Iterate over the objects in the array using map
  2. Create a new object and add the existing (key,value) pairs in the new object using the dynamic elements feature.
  3. Add the 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.

1
votes

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