简体   繁体   English

Mule Dataweave 2-数组对象的JSON字符串列表

[英]Mule Dataweave 2 - List of JSON Strings to Array Object

I get from an external API a text/plain body with the following content. 我从外部API获得具有以下内容的text/plain 主体 The output has the same format, JSON per line. 输出具有相同的格式,每行JSON。

{"update":"7.6"}
{"update":"3.2"}
{"update":"1.3"}

The output expected ( Object Array ): 预期的输出( Object Array ):

[{"version":"7.6"},{"version":"3.2"},{"version":"1.3"}]

How can I loop each String line and transform to Array of Objects? 如何循环每条字符串行并转换为对象数组?

Assuming I have to transform each line to JSON first. 假设我必须先将每行转换为JSON。

Hi your input payload is a json lines kind. 嗨,您的输入有效载荷是json行类型。 There is a simple way to support this. 有一个简单的方法可以支持这一点。

%dw 2.0
output application/json
---
payload splitBy  "\n" map ((jsonValue, index) -> read(jsonValue, "application/json"))

This will split your input by lines and read each line. 这将按行划分您的输入并读取每一行。

You can use lookup function from dataweave for converting each line to JSON. 您可以使用dataweave中的查找功能将每行转换为JSON。 Pass JSONstring as input to lookup function and return Json object. 将JSONstring作为输入传递给查找函数并返回Json对象。 Following code should works fine 以下代码应该可以正常工作

Main dataweave which takes text/plain as input but consider it as single column CSV without any header. 以文本/纯文本作为输入但将其视为没有任何标题的单列CSV的主数据编织。

<dw:transform-message doc:name="Transform Message">
    <dw:input-payload mimeType="application/csv">
        <dw:reader-property name="header" value="false"/>
        <dw:reader-property name="separator" value="|"/>
    </dw:input-payload>
    <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload map lookup('getJsonData',$.column_0)]]>
    </dw:set-payload>
</dw:transform-message>

Above script calls following lookup function which takes input as JSON string nd output as JSSON object. 上面的脚本调用以下查找函数,该函数将输入作为JSON字符串,将输出作为JSSON对象。

    <flow name="getJsonData">
        <dw:transform-message doc:name="Transform Message">
            <dw:input-payload mimeType="application/json"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload]]></dw:set-payload>
        </dw:transform-message>
    </flow>  

Hope this helps. 希望这可以帮助。

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

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