Mule (Dataweave) transform JSON array to string

Keith Fosberg picture Keith Fosberg · Jan 5, 2017 · Viewed 7.3k times · Source

This is probably simple, but everything I find in a search is only vaguely related to my question.

I have this:

{
  "user":"C03999",
  "caseNumber":"011-1234567",
  "documents":[
    {
        "file":[
            {"name":"indem1","document":"xSFSF%%GSF","mimeType":"PDF"},
            {"name":"indem2","document":"xSFSF%%GSF","mimeType":"PDF"}
            ]
        }
    ],
  "mortgagee":"22995",
  "billTo":"Originator",
  "agreementDate":"2016-11-25",
  "term":360,
  "expirationDate":"2017-11-25",
  "startDate":"Agreement",
  "lenderEndorsed":true,
  "lenderExecutedAgreement":false,
  "indemnificationAgreementTransferable":false,
  "qadLrsFileNumber":"2016-99999-9999",
  "docketNumber":"2016-9999-99"
}

I would like to get this string out of it: indem1,indem2

I created a global function:

<configuration doc:name="Configuration">
     <expression-language autoResolveVariables="true">
         <global-functions>
             def convertToString(data){
                 return data.toString();
             }
         </global-functions>
     </expression-language>
</configuration>

My transform looks like this:

%dw 1.0
%output application/csv
---
payload.documents.file map {
      "" : convertToString($.name)
}

My output looks like: [indem1\, indem2]

What do I need to do to get my desired string (indem1,indem2)?

Answer

Senthil c picture Senthil c · Jan 6, 2017

Your question title says that " JSON array To string" but the dataweave code attached above has application/csv in the output.

Are you trying to convert into csv?? If that is the expectation, comma is a reserved character for .csv format and that is the reason , is getting escaped with backslash[indem1\,indem2].

If your intention is to convert into java, here is the code that returns String value.

%dw 1.0

%output application/java

payload.documents[0].file.*name joinBy ','