mani24991
mani24991

Reputation: 23

Mule:How to combine a list of json values into one

Input JSON

{
     "digital-profiles": [{
             "Id": "INTID1",
             "status": "ACTIVE",
             "cId": "12"
         },
         {
             "dId": "INTID2",
             "status": "barred",
             "cId": "13"
         },
         {
             "Id": "INTID3",
             "status": "ACTIVE",
             "cId": "14"
         }
     ]
 }

Output:

{
     "Results": {
             "NewId": "INTID1:ACTIVE,INTID2:barred,INTID3:ACTIVE"
         }
 } 

I am trying to achieve the above mentioned output JSON using the input which is mentioned above. How to achieve this using dataweave transformation. Any help would be appreciated.

Upvotes: 1

Views: 911

Answers (2)

AnupamBhusari
AnupamBhusari

Reputation: 2415

Use map and joinBy to get desired output. This works for me

%dw 1.0
%output application/json
---
Results : {
    NewId : payload.digital-profiles map ($.Id ++ ':' ++ $.status) joinBy ','
}

Hope this helps.

Upvotes: 3

Dave Haynes
Dave Haynes

Reputation: 1

this is one way you could do it ( code not tested but working on something very similar at the moment)

function ParseJSON(MyObject)
{
// parse incoming var into a JSON object.
var MyObjectParsed= JSON.parse(MyObject);
var i = 0;
var NewJSONstring = "{/"Results/": {/"NewId/":";

// for each profile 
for(;MyObjectParsed.digital-profiles[i];)
{
    NewJSONstring = NewJSONstring+ MyObjectParsed.digital-profiles[i].Id;
    NewJSONstring = NewJSONstring+ ':';
    NewJSONstring = NewJSONstring+ MyObjectParsed.digital-profiles[i].status;
    NewJSONstring = NewJSONstring+ ',';
    i++
}

return  NewJSONstring;

Upvotes: -2

Related Questions