0
votes

I have a CSV like this:

data1,data2,data3;dataa;datab;datac;datax,datay,dataz
data1,data2,data3;dataa;datab;datac;datax,datay,dataz
data1,data2,data3;dataa;datab;datac;datax,datay,dataz

I use spliter to process the records line by line, further I use splitBy "," in dataweave to convert the record to a map. But how I can do another level of split for ";" ? SplitBy doesnt allow muliple delimiters so do the CSV type in dataweave.

Ultimately, I want a JSON like this:

{
   "1":"data1",
   "2":"data2",
   "3":{
      "a":"dataa",
      "b":"datab",
      "c":"datac"
   },
   "x":"datax",
   "y":"datay",
   "z":"dataz "
}

Any thoughts ?

2
The expected JSON result is invalid. Did you mean like this? { "1": "data1", "2": "data2", "3": { "a": "dataa", "b": "datab", "c": "datac" }, "x": "datax", "y": "datay", "z": "dataz" }sulthony h
Oops , my typo. Yes you are right .gnanagurus

2 Answers

3
votes

Try the following DataWeave code:

%dw 1.0
%output application/json
---
payload map {
    "1": $[0],
    "2": $[1],
    "3": using (detail = $[2] splitBy ";") {
        a: detail[1],
        b: detail[2],
        c: detail[3]
    },
    x: $[3],
    y: $[4],
    z: $[5]
}

Notes:

  • I modified the input data to separate datac and datax. Replace the ; character with , e.g.: ...;datab;datac,datax,...
  • I use File connector to read the CSV file, and directly process it in DataWeave transformer (do not use a Splitter)
1
votes

I want to observe, that your JSON example has bad structure! In this JSON the 4th element is an object and it hasn't a key, just value... First of all, u should validate your end JSON. Example of your valid JSON: enter image description here

When u validate your JSON, I'll try to help in convering your CSV data to the JSON.