简体   繁体   English

Mule (Dataweave) 将 JSON 数组转换为字符串

[英]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我想从中取出这个字符串: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]我的输出看起来像:[indem1\\, indem2]

What do I need to do to get my desired string (indem1,indem2)?我需要做什么才能得到我想要的字符串 (indem1,indem2)?

Your question title says that " JSON array To string" but the dataweave code attached above has application/csv in the output. 您的问题标题说“ JSON数组转换为字符串”,但上面附加的dataweave代码在输出中包含application / csv。

Are you trying to convert into csv?? 您是否要转换为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]. 如果这是期望值,则逗号是.csv格式的保留字符,这就是使用反斜杠[indem1 \\,indem2]进行转义的原因。

If your intention is to convert into java, here is the code that returns String value. 如果您打算将其转换为Java,则以下是返回String值的代码。

%dw 1.0 %dw 1.0

%output application/java %输出应用程序/ Java

payload.documents[0].file.*name joinBy ',' 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.通常,如果您有一个对象,并且需要以给定格式/MIME 类型编写该对象的一部分,请使用 dw::Core 模块中的write()函数。 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.如果您需要读取 JSON/JAVA/CSV 对象中的某些内容,而其余内容是其他格式,请使用同一模块中的read()函数。

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.此外,假设您希望对象是 JAVA,但“field2”值被视为 JSON。 You can use this DW:你可以使用这个 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.请注意,“field2”现在显示为嵌套的 JSON 对象,而不是 JAVA 对象。 One way to tell is that the key in the JSON object is in quotes, but in a JAVA object they are not.一种判断方法是 JSON 对象中的键在引号中,但在 JAVA 对象中它们不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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