Dataweave 2 - 按行输出多个 JSON

Dataweave 2 - Output multiple JSON by line

我的输入是一个对象数组,我想映射JSON对象.

我希望每个JSON对象每行一行并且JSON设置在相同的有效负载.

我的 Dataweave 代码:

 %dw 2.0
 output application/json indent = false
 ---
 payload map (payload, indexOfPayload ) -> {
     id: payload.externalid_c,
     surname: payload.surname__c
         platform: payload.platform__c
 }

我想要的示例输出:

 {"id": "demo", "surname": "anypoint", "platform": "testing"}
 {"id": "demo2", "surname": "studio", "platform": "apple"}
 {"id": "demo3", "surname": "windows", "platform": "microsoft"}

您好,您需要使用 write 函数并将其输出为纯文本,因为所需的输出无效 json

%dw 2.0
output text/plain
---
payload map ((value, index) -> write(value, "application/json", {indent: false})) joinBy  "\n"

此示例向您展示了如何操作。

write as json 首先使用 writer 属性 删除缩进,将列表项连接在一起,用新行分隔并输出为 text/plain(cannot使用 json 作为无效 json)

%dw 2.0
output text/plain
---
payload map ((item, index) -> 
    write({id: item.externalid_c,
    surname: item.surname__c,
    platform: item.platform__c
    }, "application/json", {"indent":false}) 

) joinBy  '\r'