Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Key Value Pair in DataWeave

Tags:

mule

dataweave

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
}
like image 287
Jaron Thatcher Avatar asked Feb 12 '16 16:02

Jaron Thatcher


Video Answer


2 Answers

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
}
like image 55
Tim Marinšek Avatar answered Oct 28 '22 01:10

Tim Marinšek


Can you try what is in the image below?

enter image description here

like image 24
Ralph Rimorin Avatar answered Oct 28 '22 02:10

Ralph Rimorin