I hope someone sees my post and please do help me to solve this. I am just wondering if there is a very simple code that can solve this using DataWeave.
I have this static payload being used
[
{
"products": [
{
"code": "2698791-01",
"url": "https://www.product.com/268791-01.png",
"basePrice": {
"value": 679.00,
"formattedValue": "679,00 €"
},
"name": "Product 1 Pro",
"friendlyName": "Pro"
}
]
},
{
"products": [
{
"code": "9696379-01",
"url": "https://www.product.com/966379-01.png",
"basePrice": {
"value": 2.90,
"formattedValue": "2,90 €"
},
"name": "Product 2 Ultra",
"friendlyName": "Ultra"
}
]
},
{
"products": [
{
"code": "9696377-01",
"url": "https://www.product.com/966377-01.png",
"basePrice": {
"value": 72.90,
"formattedValue": "72,90 €"
},
"name": "Product 3 Plus",
"friendlyName": "Plus"
}
]
}
]
This is my code and it is a working code
%dw 2.0
output application/json
var id = "1234"
var code = "9696379-01"
var idObj = {
"currency": "EUR",
"id": id
}
var productObj = payload
var fileData = productObj filter ((item, index) -> item.products.code == [code])
---
idObj ++ fileData[0]
If using only one "code" (see the code value), the result will be
{
"currency": "EUR",
"id": "1234",
"products": [
{
"code": "9696379-01",
"url": "https://www.product.com/966379-01.png",
"basePrice": {
"value": 2.90,
"formattedValue": "2,90 €"
},
"name": "Product 2 Ultra",
"friendlyName": "Ultra"
}
]
}
But, if the "code" will have multiple values like this var code = "2698791-01, 9696379-01, 9696377-01"
This should be the result:
{
"currency": "EUR",
"id": "1234",
"products": [
{
"code": "2698791-01",
"url": "https://www.product.com/268791-01.png",
"basePrice": {
"value": 679.00,
"formattedValue": "679,00 €"
},
"name": "Product 1 Pro",
"friendlyName": "Pro"
},
{
"code": "9696379-01",
"url": "https://www.product.com/966379-01.png",
"basePrice": {
"value": 2.90,
"formattedValue": "2,90 €"
},
"name": "Product 2 Ultra",
"friendlyName": "Ultra"
},
{
"code": "9696377-01",
"url": "https://www.product.com/966377-01.png",
"basePrice": {
"value": 72.90,
"formattedValue": "72,90 €"
},
"name": "Product 3 Plus",
"friendlyName": "Plus"
}
]
}```
Can someone help me please to solve this problem?
Your scripts assumes a single code in the condition. For a list you can change the condition to 'list of valid codes contain the code in the payload'. It can be implemented using the function contains()
.
I'm assuming that since products
is an array there could be more than one product in it, then every product must have a valid code. Change the condition of the filter if the assumption is wrong. I used the every()
function from the Arrays module.
I also put the list of codes as an Array instead of a String. If you must use the string for some reason then use splitBy()
to separate the codes into an Array.
%dw 2.0
output application/json
import * from dw::core::Arrays
var id = "1234"
var code = ["2698791-01", "9696379-01", "9696377-01"]
var idObj = {
"currency": "EUR",
"id": id
}
var fileData =
payload
filter ((item, index) ->
item.products every(code contains $.code ) // assuming more than one product in products then every product must have a valid code.
)
flatMap ($.products)
---
idObj ++ { products: fileData }