1

I want to generate a CSV output based on "||" as separator. I tried concatenation and other different approaches but I'm not able to achieve the desired output.

Input payload:

[
    {

        "details": "products' details; LED; 10-34V; 90 CRI; 300j-400h; 708-150 lm; 6 in.AVAIL",
        "Id": "AgXS31456",
        "age": "25"
    }
]

Used script:

%dw 2.0
output application/csv  header=false, separator="|"

---
payload map ((item) -> {
    "field_1": item.Id ,
    "field_2": item.details,
    "field_3": item.age
    
})

Expected output:

AgXS31456||products' details; LED; 10-34V; 90 CRI; 300j-400h; 708-150 lm; 6 in.AVAIL||25

Not sure if there another way to twist the script in order to get the delimiter separator as expected.

aled
  • 21,330
  • 3
  • 27
  • 34
james11
  • 55
  • 6
  • 1
    Can you update the title to say "Multi character delimiter". This looks like it can be a common problem and correct title can help other landing on this question – Harshank Bansal Jul 25 '23 at 10:50

1 Answers1

2

The separator writer property of the DataWeave CSV format only uses a single character as separator. If you try to have a multicharacter separator it only takes the first one. To achieve what you want we'll need to do a hack and concatenate or join each field into a single string field. The field name is not relevant since you are not printing headers.

%dw 2.0
output application/csv  header=false
---
payload map ((item) -> {
    "field_1": [item.Id , item.details, item.age] joinBy "||"
}) 

Output:

AgXS31456||products' details; LED; 10-34V; 90 CRI; 300j-400h; 708-150 lm; 6 in.AVAIL||25
aled
  • 21,330
  • 3
  • 27
  • 34