Single-value selector
Exercise
Section titled “Exercise”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
firstNameandlastName.
Show solution
%dw 2.0output application/json---{ id: payload.id, name: payload.firstName ++ " " ++ payload.lastName, age: payload.age, address: payload.address.address}Notes:
- The
++function lets you join values (in this caseStrings), which is what lets us joinfirstNameandlastName. - The
addressproperty inside thepayloadis an object, so we can use the single-value selector again to get a value inside it (in this case theaddresskey shows up once more).
{Code mat;}
Learn MuleSoft: courses, exercises and quizzes. Free, no sign-up.
© 2026 Codebymat