how to write data weave code for given input and output

Viewed 17

I want to reduce or replace this [] with {}

input: ["abcd,"whyf","thsr"]

output: {"abcd,"whyf","thsr"}

2 Answers

Assuming the input is in JSON it means it is an array. It could be just a string too. The output is not a valid JSON document, because starting with a curly brackets it means it should be an object, but it misses keys for each value.

If for some reason you are intending to just output a string with the square brackets replaced with the curly brackets, then you can transform the input as a string and then use string replaces to change the characters, but be aware that the output is not valid JSON, nor any other supported structured format.

%dw 2.0
output application/java
---
write(payload,"application/json",{indent:false}) replace /\[/ with("{") replace /]/ with("}")

Output (as a string):

{"abcd","whyf","thsr"}

Assuming the output to be string you can also try the following which uses joinBy

%dw 2.0
output text/plain
---
'{"' ++ (payload joinBy '","') ++ '"}'
Related