vincentlowe icon

APDev Dataweave multi-record example

vincentlowe | PRO | 02/10/19 08:49:32 PM UTC | 0 ⭐ | 271 👁️ | Never ⏰ | []
text |

990 B

|

None

|

0 👍

/

0 👎

%dw 2.0
output application/json
type Currency = String {format: "###.00"}
type Flight = Object {class: "com.mulesoft.training.Flight"}
 //var numSeats = 400
//var numSeats = (x=400) -> x
/*var numSeats = (planeType: String) -> 
	if (planeType contains('737'))
		150
	else
		300
*/
fun getNumSeats(planeType: String) =
	if (planeType contains('737'))
		150
	else
		300
 fun normalizeModelName (planeType: String) =
	planeType replace /Boing/ with "Boeing"
---
using (flights = 
	payload..*return map (object, index) -> {
		destination: object.destination,
		price: object.price as Number as Currency,
		totalSeats: getNumSeats(object.planeType as String),
		availableSeats: object.emptySeats,
		planeType: upper(normalizeModelName(object.planeType)),
		departureDate: object.departureDate 
			as Date {format: "yyyy/MM/dd"} 
			as String {format: "MMM dd, yyyy"}
	} as Flight
)
 flights orderBy $.departureDate orderBy $.price
	distinctBy $ filter ($.availableSeats != "0")

Comments