Dynamic Key Value Pair in DataWeave
Asked Answered
H

7

6

DataWeave doesn't like what I'm trying to do with it, and I'm not sure if I'm doing something wrong, or if it is a limitation of DataWeave that isn't possible.

Here's the scenario: I'm querying Salesforce and getting two values back: lets call them X and Y.

Here's the return I want [{X:Y}, {X2:Y2}, {X3:Y3}, ...] however, using DataWeave it doesnt seem possible to get a key value pair like that, instead, it only seems possible to specifically set the Key for each value in the script like so: [{Value_X: X, Value_Y: Y}, {Value_X: X2, Value_Y: Y2}, ...]

Here is my current DataWeave script that works, but gives me the second result:

%dw 1.0
%output application/java
---

payload map {
    Value_X: $.X,
    Value_Y: $.Y
}

And here's the DataWeave script that I wish worked, but doesn't

%dw 1.0
%output application/java
---

payload map {
    $.X: $.Y
}
Hydrous answered 12/2, 2016 at 16:38 Comment(0)
P
15

In order for your Dataweave code to work properly, you need to surround the variable you want to use as a key with parentheses:

%dw 1.0
%output application/java
---

payload map {
    ($.X): $.Y
}
Phlebitis answered 11/3, 2016 at 16:11 Comment(0)
D
8

Can you try what is in the image below?

enter image description here

Drandell answered 12/2, 2016 at 19:9 Comment(1)
I think you may be on the right track, and I think this would work for a normal input, but Salesforce returns a ConsumerIterator, and DataWeave throws this exception 1. Cannot coerce a :iterator to a :object (com.mulesoft.weave.model.values.coercion.exception.UnsupportedTypeCoercionExc eption)Hydrous
M
0

Data Weave supports conditional key value pairs.

Look at this documentation https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-types#conditional-elements-2

Mistook answered 25/1, 2017 at 20:18 Comment(1)
link is (now) broken, oonly returns page not found.Urrutia
U
0

In my case, I have column names separate from column values.

<results>
  <meta-data>
    <column-label>X1</column-label>
    <column-label>X2</column-label>
  </meta-data>
  <data>
    <column-value>Y1</column-value>
    <column-value>Y2</column-value>
  </data>
</results>

The following dwl layout worked:

%dw 1.0
%output application/json
---
using (y= payload.results)
y.data map using (x= payload.results.meta-data[$$]) {
  (x): $
}
User answered 8/2, 2017 at 14:10 Comment(0)
B
0

For Dynamic key:value pair mapping mapobject is the only way.

Please read below link for more information related to mapobject

https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#map-object

Bayless answered 8/3, 2018 at 11:29 Comment(0)
H
-2

It seems like DataWeave is unable to do this from my experiments. I did get it to work using a Python scripting transformer. Here is the XML that will do this translation properly:

<scripting:transformer doc:name="Python">
<scripting:script engine="jython">
<![CDATA[
    map = {}
    while (payload.hasNext()):
        next = payload.next()
        map[next['X']] = next['Y']
    result = map
]]>
</scripting:script>
</scripting:transformer>
Hydrous answered 12/2, 2016 at 18:12 Comment(0)
S
-2

The simplest way is (payload01.Notification.dynamicProperties map (payload02, indexofPayload02) -> { (payload02.name) : payload02.value })

Subtend answered 8/2, 2017 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.