Home > Software design >  Mule issue mapping payload dataweave 2.0
Mule issue mapping payload dataweave 2.0

Time:01-27

I am trying to map the below json payload to only show dates with "999" value. Below is my payload

 
 {
  "Delivery Date": "16",
  "17/01/2018": "0",
  "18/01/2018": "999",
  "19/01/2018": "999",
  "20/01/2018": "0",
  "29/01/2018": "999",
  "18/02/2018": "0",
  "19/02/2018": "999",
  "20/02/2018": "999",
  "03/03/2018": "999"
 }
 ]

And my desired output is

{ 
  "available_date": [ 
    "18/01/2018", 
    "19/01/2018, 
    "29/01/2018", 
    "19/02/2018", 
    "20/02/2018", 
    "03/03/2018"
  ],
  "message": "" 
}

Thanks for any advice!

CodePudding user response:

I have tried this.

Approach 1 MapObject, Pluck

%dw 2.0
output application/json
---
{
"available_date":
    (payload mapObject ((value, key, index) -> ((key): (value)) if (value == "999")) pluck $$),
"message":""
}

Approach 2 FilterObject, keysOf

%dw 2.0
output application/json
---
{
"available_date": keysOf(payload filterObject ((value, key) -> value == "999")),
"message":""
}

Output

{
  "available_date": [
    "18/01/2018",
    "19/01/2018",
    "29/01/2018",
    "19/02/2018",
    "20/02/2018",
    "03/03/2018"
  ],
  "message": ""
}

CodePudding user response:

Alternative using filterObject and keysOf:

%dw 2.0
output application/json
---
{
    'available_date' : keysOf(payload filterObject ((value, key, index) -> value contains "999")),
    message: ""
}
  •  Tags:  
  • Related