propertiesdataweavemulesoft

Mulesoft Dataweave use a variable as item in a selection at a JSON object


I want to do a selection on an item based on a (yaml) property in Dataweave. I have this code:

%dw 2.0

var testWithProperty = p('incoming.var')
// incoming.var  refers to the value "resultSet"

var inPayload = [
    resultSet: {
        id: "some Id"
    },
    resultSet: {
        id: "some other Id"
    }
]

output application/json
---
{
    "result1": inPayload..resultSet,
    "result2": inPayload..testWithProperty
}

result right now is like this:

{
  "result1": [
    {
      "id": "some Id"
    },
    {
      "id": "some other Id"
    }
  ],
  "result2": null
}

result2 is null.

The result should be this:

{
  "result1": [
    {
      "id": "some Id"
    },
    {
      "id": "some other Id"
    }
  ],
  "result2": [
    {
      "id": "some Id"
    },
    {
      "id": "some other Id"
    }
  ]
}

How can I use a property in such a case on a selection ?


Solution

  • You need to use Dynamic Selector for this.

    {
        "result1": inPayload..resultSet,
        "result2": inPayload..[testWithProperty],
        "result3": inPayload.."$(testWithProperty)"
    }
    

    When you do inPayload..testWithProperty it will look for the property testWithProperty and will not resolve the testWithProperty as a variable. When you use Dynamic Selector inPayload..[testWithProperty] OR inPayload..'$(testWithProperty)' it the value will be evaluated as you need.