admin管理员组

文章数量:1279234

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 ?

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 ?

Share Improve this question edited Feb 25 at 0:11 eglease 2,75411 gold badges20 silver badges34 bronze badges asked Feb 24 at 9:55 BenBen 7421 gold badge11 silver badges34 bronze badges 1
  • Using the descendant selector will match any key with the expected value, even several levels nested. If you know that the expected key is at the first level and repeats just use the multi valued selector: inPayload.*resultSet. – aled Commented Feb 24 at 12:54
Add a comment  | 

1 Answer 1

Reset to default 2

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.

本文标签: propertiesMulesoft Dataweave use a variable as item in a selection at a JSON objectStack Overflow