muledataweaveanypoint-studiomule4mule-esb

DataWeave expression returning unwanted array


I have the following DataWeave expression:

"extension": [
        {
            "url": "insurance-product/link",
            "valueUrl": inObject.InsuranceProductLink
            },
        },
        flatten(vars.contactPoints filter ($.InsuranceProductId == inObject.InsuranceProductId) map (item, index) -> {
            extension: [
                {
                    url: "gatekeeper",
                    valueBoolean: item.ContactPointIsGatekeeper
                },
                {
                    url: "rank",
                    valuePositiveInt: item.ContactPointRank
                },
                {
                    url: "contact",
                    valueReference: {
                        reference: "todo"
                    }
                },
                {
                    url: "type",
                    valueCode: item.ContactPointType
                }
            ]
        })
    ]

That returns this result:

"extension": [
    {
        "url": "insurance-product/link",
        "valueUrl": "redacted"
    },
    [
        {
            "extension": [
                {
                    "url": "gatekeeper",
                    "valueBoolean": false
                },
                {
                    "url": "rank",
                    "valuePositiveInt": 3
                },
                {
                    "url": "contact",
                    "valueReference": {
                        "reference": "todo"
                    }
                },
                {
                    "url": "type",
                    "valueCode": "app"
                }
            ]
        },
        {
            "extension": [
                {
                    "url": "gatekeeper",
                    "valueBoolean": true
                },
                {
                    "url": "rank",
                    "valuePositiveInt": 2
                },
                {
                    "url": "contact",
                    "valueReference": {
                        "reference": "todo"
                    }
                },
                {
                    "url": "type",
                    "valueCode": "general-practitioner"
                }
            ]
        }
    ]
]

But I need this structure as the output:

"extension": [
    {
        "url": "insurance-product/link",
        "valueUrl": "redacted"
    },
    {
        "extension": [
            {
                "url": "gatekeeper",
                "valueBoolean": false
            },
            {
                "url": "rank",
                "valuePositiveInt": 3
            },
            {
                "url": "contact",
                "valueReference": {
                    "reference": "todo"
                }
            },
            {
                "url": "type",
                "valueCode": "app"
            }
        ]
    },
    {
        "extension": [
            {
                "url": "gatekeeper",
                "valueBoolean": true
            },
            {
                "url": "rank",
                "valuePositiveInt": 2
            },
            {
                "url": "contact",
                "valueReference": {
                    "reference": "todo"
                }
            },
            {
                "url": "type",
                "valueCode": "general-practitioner"
            }
        ]
    }
]

So that the inner extensions are not elements of an array, but just elements.

I've tried a lot of different things but cannot get it in this structure. What do I have to modify, so that the desired output is achieved?


Solution

  • The expression starting with flatten() will return an array -because flatten() returns an array- so that is to be expected. Instead you can make the first element an array and concatenate it with the result of the other expression.

    {   // the first curly bracket was missing
       "extension": [
            {
                "url": "insurance-product/link",
                "valueUrl": inObject.InsuranceProductLink
            }] // close the array
            ++  // concatenate both arrays
            flatten(vars...
    

    In that why the result will be a single array.