How to convert an Array into Object?

Step 1:

Add the transform Message Component in the mule flow from component palette.

Step 2:  

In this component we can write the Dataweave script to convert array into object. We can click on Preview button to see the resultant of Dataweave operation.

Note:  The array enclosed in evaluation parentheses and added to the constructor curly braces can convert the array into object.

Dataweave Expression:

%dw2.0

outputapplication/json

vararr=[one:1,two:2]

{

      (arr)

}

Output

{

“one”: 1,

“two”: 2

}

Step 3:

Above output can also be achieved using the reduce function in Dataweave.

Dataweave Expression:

%dw2.0

outputapplication/json

vararr=[one:1,two:2]

arrreduce (value,acc={})->acc++value

Output

{

“one”: 1,

“two”: 2

}

Reference Link: https://docs.mulesoft.com/mule-runtime/4.3/

Leave a Reply

Your email address will not be published. Required fields are marked *