Warm tip: This article is reproduced from serverfault.com, please click

Dataweave

发布于 2019-06-24 15:46:42

My input is an Array of Java Objects:

[{"name"="Demo","platform"=[{"id"="1","value"="ios"},{"id"="2","value"="android"}],"language"=[{"id"="1","value"="eng"}],"date"="20/05/2018"}, {"name"="Kernel","platform"=[{"id"="1","value"="macos"},{"id"="2","value"="linux"}],"language"=[{"id"="1","value"="ger"}],"date"="20/05/2018"}]

Each Java Object contains arrays in Platform and language key like this example:

{"name"="Demo","platform"=[{"id"="1","value"="ios"},{"id"="2","value"="android"}],"language"=[{"id"="1","value"="eng"}],"date"="20/05/2018"}

This is the output expected in text/plain type:

{"name":"Demo","platform":[{"id":"1","value":"ios"},{"id":"2","value":"android"}],"language":[{"id":"1","value":"eng"}],"date":"20/05/2018"}
{"name":"Kernel","platform":[{"id":"1","value":"macos"},{"id":"2","value":"linux"}],"language":[{"id":"1","value":"ger"}],"date":"20/05/2018"}
  • Each object from Java to JSON
  • Indent=false per line
  • No brackets or commas between objects. Each object per line
  • Must not affect Platform and Language key arrays

Is it possible to apply this without having to play with String replace?

Questioner
gtx911
Viewed
0
machaval 2019-06-25 05:41:30

So you can use the write function to transform each element to application/json and with the writer property indent=false

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

Or if you use mule 4.2 you can directly use application/x-ndjson and it should work directly

%dw 2.0
output application/x-ndjson
---
payload