简体   繁体   中英

Mule (Dataweave) transform JSON array to string

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)?

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 ','

In general, if you have an object, and you need PART of that object to be written in a given format / MIME-type, use the write() function in the dw::Core module. If you need to read some content in an object that is JSON/JAVA/CSV and the rest is some other format, use the read() function in the same module.

For example, suppose you have payload as:

{
  field1: "foo",
  field2: { nested: "value" }
}

Further, suppose that you want the object to be JAVA, but the "field2" value to be seen as JSON. You can use this DW:

output application/java
---
{
  field1: payload.field1,
  field2: write(payload.field2,'application/json')
}

The result is:

{
  field1: "foo",
  field2: '{ "nested": "value" }'
}

NOTICE that "field2" now shows up as a nested JSON object, not a JAVA object. One way to tell is that the key in the JSON object is in quotes, but in a JAVA object they are not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM