0
votes

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"}
2

2 Answers

3
votes

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.

3
votes

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'