Search code examples
muletostringdataweavetext-indent

JSON Object to String with carriage-return


In my flow, I read and run (for-each) an array of JSON objects. I insert each object in the same file with APPEND type.

enter image description here

I want to store each JSON with indent as false (one line by JSON) and a carriage-return like this example:

{"hello:"world1"}

{"hello:"world2"}

{"hello:"world3"}

I use this:

%dw 2.0
 output application/json indent=false
 ---
 payload ++ '\r'

But it returns an error about cannot coerce Object to String. How can I solve this?


Solution

  • application/json is technically an object, not a string. So you cannot concatenate directly.

    This works for me, to get the desired result:

    %dw 2.0
    output application/java
    ---
    write(payload, "application/json", {"indent":false}) as String ++ '\r'
    

    write as json first to use the writer property to remove indentation then convert to string and concatenate and output as String application/java