Skip to content
ENG

Single-value selector

Given the following input payload:

{
"id": 1,
"firstName": "Emily",
"lastName": "Johnson",
"maidenName": "Smith",
"age": 29,
"gender": "female",
"email": "emily.johnson@x.dummyjson.com",
"phone": "+81 965-431-3024",
"username": "emilys",
"password": "emilyspass",
"birthDate": "1996-5-30",
"bloodGroup": "O-",
"height": 193.24,
"weight": 63.16,
"eyeColor": "Green",
"hair": {
"color": "Brown",
"type": "Curly"
},
"address": {
"address": "626 Main Street",
"city": "Phoenix",
"state": "Mississippi",
"stateCode": "MS",
"postalCode": "29112",
"coordinates": {
"lat": -77.16213,
"lng": -92.084824
},
"country": "United States"
}
}

Write a DataWeave transformation that produces the following output:

{
"id": 1,
"name": "Emily Johnson",
"age": 29,
"address": "626 Main Street"
}
  • Use the single-value selector (.) to reach the value of a key in the payload.
  • Remember that you can nest selectors if the returned response allows it: payload.key.otherKey.
  • The name is a concatenation of firstName and lastName.

🔒🔓Show solution
%dw 2.0
output application/json
---
{
id: payload.id,
name: payload.firstName ++ " " ++ payload.lastName,
age: payload.age,
address: payload.address.address
}

Notes:

  1. The ++ function lets you join values (in this case Strings), which is what lets us join firstName and lastName.
  2. The address property inside the payload is an object, so we can use the single-value selector again to get a value inside it (in this case the address key shows up once more).
{Code mat;}

Learn MuleSoft: courses, exercises and quizzes. Free, no sign-up.