Search code examples
mulemule-studiomule-esb

Creating a single json from a combined json list


I am getting multiple json files in a for loop and each json in each loop are in a format :

{
    "myproperties": {
        "http.port": "8088",
        "http.base": "/abc",
        "http.path": "test"
    },
    "information": [{
        "abc": {
            "key1": "ghghghghgh"
        },
        "efg": {
            "key1": "value1"
        }
    }]
}

and

{
    "myproperties": {
        "http.port": "6789",
        "db.base": "tat",
        "db.path": "ghghghg"
    },
    "information": [{
        "efg": {
            "key1": "ghghghghgh"
        },
        "ijk": {
            "key1": "value1"
        }
    }]
}

and so on ……….

I manage to combine all the json in a list out side the for loop and the combine json list looks like:

    [{
    "myproperties": {
        "http.port": "8088",
        "http.base": "/abc",
        "http.path": "test"
    },
    "information": [{
        "abc": {
            "key1": "ghghghghgh"
        },
        "efg": {
            "key1": "value1"
        }
    }]
}, 
 {
    "myproperties": {
        "http.port": "6789",
        "db.base": "tat",
        "db.path": "ghghghg"
    },
    "information": [{
        "efg": {
            "key1": "ghghghghgh"
        },
        "ijk": {
            "key1": "value1"
        }
    }]
}]

Now I want to make **single** json output out of this combine json something in a following format:

 {
    "myproperties": {
        "http.port": "6789",
        "db.base": "tat",
        "db.path": "ghghghg",
        "http.base": "/abc",
        "http.path": "test"
    },
    "information": [{
            "efg": {
                "key1": "ghghghghgh"
            },
            "ijk": {
                "key1": "value1"
            }
        },
        {
            "abc": {
                "key1": "ghghghghgh"
            },
            "efg": {
                "key1": "value1"
            }
        }
    ]
 }

please note in the myproperties section only unique and distinct node is there. I am not sure how to begin this with dataweave… any pointer will be appreciated,

I tried the following :

%dw 1.0
%output application/json skipNullOn="everywhere",encoding="UTF-8"
---
payload map {

    myproperties: $.myproperties,
    information: $.information

}

But not working

Thanks


Solution

  • Try mapObject for combining properties and flatten for changing information array to single array like

    %dw 1.0
    %output application/json
    ---
    {
        myproperties : payload.myproperties mapObject $ ,
        information : (flatten payload.information) 
    }
    

    HTH

    Update:- For distinct properties you have to get distinct properties first and then map all distinct properties. But you may loose duplicate properties. refer below

    %dw 1.0
    %output application/json
    %var distinctProperties =((payload.myproperties mapObject $) pluck $$)  distinctBy $ 
    %var aggregatedPropertiesMap =  (payload.myproperties mapObject $)
    ---
    {
        myproperties : {( distinctProperties map { // interate over distinct properties 
            ($) : aggregatedPropertiesMap[$]    // map property as key and fetch its value from aggregatedPropertiesMap
        })} ,
        information : (flatten payload.information) 
    }