Single-value selector
Context
Section titled “Context”In DataWeave, selectors are the main way to navigate a payload and pull data out of it.
The most basic one is the single-value selector (.), which reads a property of an object by name.
Syntax
Section titled “Syntax”payload.propertyNameIt works like reading a key from a JSON object. If the payload is:
{ "nombre": "Matías", "edad": 28, "ciudad": "Buenos Aires"}then payload.nombre returns "Matías".
Exercise
Section titled “Exercise”Given the following input payload:
{ "producto": { "id": "PROD-001", "nombre": "API Gateway", "precio": 150.00, "moneda": "USD", "activo": true }}Write a DataWeave transformation that produces this output:
{ "nombreProducto": "API Gateway", "precioFinal": 150.00}- Use the single-value selector (
.) to reach the properties of theproductoobject. - Selectors can be chained:
payload.producto.nombre.
Show solution
The DataWeave transformation would be:
%dw 2.0output application/json---nombreProducto: payload.producto.nombre,precioFinal: payload.producto.precioExplanation:
payload.productoreaches the nestedproductoobject..nombreand.precioare single-value selectors that read each property.- A new object is built with the keys
nombreProductoandprecioFinal.
Check yourself
Section titled “Check yourself”Answer these questions to confirm what you learned:
1. Which operator is the single-value selector in DataWeave?
2. Given a payload with a nested object, what does payload.a.b return if b is 42?
3. What happens if you use a single-value selector on a key that doesn't exist?
Result