0

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 Answers 2

3

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'
Sign up to request clarification or add additional context in comments.

2 Comments

It returns "Message : "You called the function 'map' with these arguments: 1: Binary ("" as Binary {mediaType: "*/*; charset=UTF-8", encoding: "UTF-8", mimeType: "...) 2: Function ((item:Any, index:Any) -> ???) But it expects arguments of these types: 1: Array 2: Function
That is because your input payload doesn't have the mimetype set. Check that on the source of the payload you specify is application/json
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.