Search code examples
muledataweavemulesoftmule4

How to map an array within an array and achieve the following output?


In the given input,

{
    "editable": true,
    "sections": [
        {
            "title": "Identification",
            "calingaKey": "",
            "content": [
                [{
                    "name": "Classification",
                    "text": "Product",
                    "url": "",
                    "info": ""
                },
                {
                    "name": "Product Number",
                    "text": "####1234",
                    "url": "",
                    "info": ""
                }]
            ]
        },
        {
            "title": "Position and Contact",
            "calingaKey": "",
            "content": [
                [{
                    "name": "Manufacturer",
                    "text": "Value of Manufacturer",
                    "url": "",
                    "info": ""
                },
                {
                    "name": "Hardware Version",
                    "text": "####1234",
                    "url": "",
                    "info": ""
                }]
            ]
        }
    ]
}

"content" is an array of array of objects. Basically, the "name" field has to be replaced by values stored in their corresponding keys in the "calinga" variable.

I could do it for the "title" field, but each "name" field should also be replaced by it's name in the variable.

%dw 2.0
output application/json
var calinga = {
    "Identification": "Identifikation",
    "Position and Contact": "Positions und Contacts",
    "Classification": "Classifikation",
    "Product Number": "Produkt Number",
    "Manufacturer": "Manufakturer",
    "Hardware Version": "Hware Vsion"
}
---
{
"editable": payload.editable,
"sections": payload.sections map(item01, index01)->{
    "title": calinga[item01.title],
    "content": item01.content map(item02)->(item02)
}
}

How Can I achieve the following output?

{
    "editable": true,
    "sections": [
        {
            "title": "Identifikation",
            "calingaKey": "",
            "content": [
                [{
                    "name": "Classifikation",
                    "text": "Product",
                    "url": "",
                    "info": ""
                },
                {
                    "name": "Produkt Number",
                    "text": "####1234",
                    "url": "",
                    "info": ""
                }]
            ]
        },
        {
            "title": "Positions und Contacts",
            "calingaKey": "",
            "content": [
                [{
                    "name": "Manufakturer",
                    "text": "Value of Manufacturer",
                    "url": "",
                    "info": ""
                },
                {
                    "name": "Hware Vsion",
                    "text": "####1234",
                    "url": "",
                    "info": ""
                }]
            ]
        }
    ]
}

Solution

  • You can use mapObject() once you descend from the last nested array into objects. Then the trick is to use the value of calinga but if it null because the key is not present then use the original value as the default: item03 mapObject {($$):calinga[$] default $}.

    Example:

    %dw 2.0
    output application/json
    var calinga = {
        "Identification": "Identifikation",
        "Position and Contact": "Positions und Contacts",
        "Classification": "Classifikation",
        "Product Number": "Produkt Number",
        "Manufacturer": "Manufakturer",
        "Hardware Version": "Hware Vsion"
    }
    ---
    {
    "editable": payload.editable,
    "sections": payload.sections map(item01, index01)->{
        "title": calinga[item01.title],
        "content": item01.content map(item02)->(item02 map(item03)-> item03 mapObject {($$):calinga[$] default $})
        }
    }