Mule ESB: How to convert Binary form of payload to array in mule 4, dw 2.0

4.7k views Asked by At

I have a payload which is coming as a binary output. I need to convert this in array format.

"{\n  \"numberRecordsFailed\": 0,
\n  \"numberRecordsProcessed\": 0,
\n  \"stateMessage\": null,
\n  \"systemModstamp\": \"2020-10-05T03:27:28\",
\n  \"apiActiveProcessingTime\": 0,
\n  \"jobId\": \"7501q000005RsosAAC\",
\n  \"apexProcessingTime\": 0,
\n  \"createdDate\": \"2020-10-05T03:27:28\",
\n  \"id\": \"7511q000005TAMFAA4\",
\n  \"state\": \"Queued\",
\n  \"totalProcessingTime\": 0\n

}"

how to convert this binary payload to array...can anyone help me with this please

below is my dw2.0 expression to extract id and state

payload map {
CLIENT_ORDER_HEADER_ID: $.payload.id,
STATUS: $.payload.state

}

1

There are 1 answers

0
AudioBubble On

I assume you get the data as a Java String. Replace data with payload in the expression below and you can give it a try:

%dw 2.0
output application/json

var data = "{\n  \"numberRecordsFailed\": 0,
    \n  \"numberRecordsProcessed\": 0,
    \n  \"stateMessage\": null,
    \n  \"systemModstamp\": \"2020-10-05T03:27:28\",
    \n  \"apiActiveProcessingTime\": 0,
    \n  \"jobId\": \"7501q000005RsosAAC\",
    \n  \"apexProcessingTime\": 0,
    \n  \"createdDate\": \"2020-10-05T03:27:28\",
    \n  \"id\": \"7511q000005TAMFAA4\",
    \n  \"state\": \"Queued\",
    \n  \"totalProcessingTime\": 0\n
    }"

var parsedData = read(data,"application/json")
---
{
    CLIENT_ORDER_HEADER_ID: parsedData.id,
    STATUS: parsedData.state
}

Note, the sample data you provided is not an array but an object. No need to use map.