Skip to content
ENG

Single-value selector

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.

payload.propertyName

It 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".


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 the producto object.
  • Selectors can be chained: payload.producto.nombre.

🔒🔓Show solution

The DataWeave transformation would be:

%dw 2.0
output application/json
---
nombreProducto: payload.producto.nombre,
precioFinal: payload.producto.precio

Explanation:

  1. payload.producto reaches the nested producto object.
  2. .nombre and .precio are single-value selectors that read each property.
  3. A new object is built with the keys nombreProducto and precioFinal.

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