2
votes

I have a HTTP requester in my flow which calls AmericanFlightsAPI, its original GET response is like this:

[{
"ID": 1,
"code": "ER38sd",
"price": 400.00,
"departureDate": "2016/03/20",
"origin": "MUA",
"destination": "SFO",
"emptySeats": 0,
"plane": {
    "type": "Boeing 737",
    "totalSeats": 150
}

I want to filter these records and show only records containing flights having destination "SFO" and "CLE", like this:

{
  "flight_ID": 4,
  "code": "rree1000",
  "destination": {
    "destination": "CLE"
  }

Other fields for the filtered destination like ID, Code should also be removed from the response. What is the Dataweave code to achieve this? Actual API is like this: Original API

1

1 Answers

3
votes

Quick and easy way could be ..

%dw 2.0
output application/json
---
payload filter ((value, index) -> (value.destination == 'SFO' or value.destination == 'CLE')) map {
    "flight_ID" : $.ID,
    "code": $.code,
    "destination": {
       "destination": $.destination
  }
}