gtx911
gtx911

Reputation: 1289

Dataweave 2 - Output multiple JSON by line

My input is a Array of Objects that I want to map to a JSON object.

I want each JSON object to be line per line and the JSON set in the same payload.

My Dataweave code:

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

Example output I want:

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

Upvotes: 0

Views: 2452

Answers (2)

Ryan Carter
Ryan Carter

Reputation: 11606

write as json first to use the writer property to remove indentation, join the list items together seperated by a new line and output as text/plain(cannot use json as its not valid 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'

Upvotes: 3

machaval
machaval

Reputation: 5059

Hi you need to use the write function and output it as text plain as the desire output is not a valid json

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

This example shows you how to do it.

Upvotes: 3

Related Questions