1

Im am new to MuleSoft.I am trying to transform a JSON payload,using transform. I want to transform my payload as below

Input:

{
    "ResponseStatus": {
        "Status": "SUCCESS",
        "StatusText": "SUCCESS"
    },
    "Processes": {
        "Process": [
            {
                "ProcessId": "1234567",
                "ProcessProperties": {
                    "Property": [
                        {
                            "Name": "XXXXXXXXXXX",
                            "Value": "11111111",
                            "Desc": "YYYYYYYY"
                        },
                        {
                            "Name": "AAAAAAAAA",
                            "Value": "2222222",
                            "Desc": "BBBBBBBB"
                        },
                        {
                            "Name": "QQQQQQQQQ",
                            "Value": "#######",
                            "Desc": "CCCCCCCC"
                        },
                        {
                            "Name": "NNNNNNN",
                            "Value": "IIIIIIII",
                            "Desc": "UYUYUYUY"
                        }
                    ]
                },
                "EditMode": "CCCCCC",
                "ProcessType": "ABCD",
                "AppName": "VFVFVGBG",
                "StatusHistory": {
                    "STS": [
                        {
                            "Sts": "COMPLETED"
                        }
                    ]
                }
            }
        ]
    }
}

Output:

[
  {
    "ProcessId": "1234567",
    "AAAAAAAAA": "2222222",
    "QQQQQQQQQ": "#######"
  }
]

I have read DWL reference from below Mulesoft link.Also reffered this SO link.

Below is what I have tried so far,

%dw 1.0
%output application/json
---
{
"ProcessId": (payload.Processes.Process.ProcessId)[0],

AAAAAAAAA: {
                        (payload.Processes.Process.ProcessProperties.Property mapObject {
($.Name):$.Value when $.Name =="AAAAAAAAA" otherwise ""
                        })
            },
   QQQQQQQQQ: {

        (payload.Processes.Process.ProcessProperties.Property mapObject {
($.Name):$.Value when $.Name =="QQQQQQQQQ" otherwise ""
                         })
            }
}

I am still not able to get the desired output.

It gives me "Cannot coerce a :array to a :key"

Can anyone please help me?

1 Answers1

0

The "property" json element in your input json is "Array",which it is not able to parse to a single value.

Please try below snippet and let me know if that gives your deisred o/p.

payload.Processes.Process map (
(val , index) ->

    {"ProcessId":(payload.Processes.Process.ProcessId)[0]
     ,

    (val.ProcessProperties.Property map {

        (($.Name) : $.Value) when  $.Name =='AAAAAAAAA'  }

    ),

    (val.ProcessProperties.Property map {

        (($.Name) : $.Value) when  $.Name =='QQQQQQQQQ'  }

    )

}
)
Mahesh_Loya
  • 2,743
  • 3
  • 16
  • 28