简体   繁体   中英

How to Convert CSV to JSON in Dataweave 2.0 in mulesoft without specific headers?

I have CSV input file without headers

Input example are as follows

ETM, St. Petersburg
Date: 03/28/12
Number: 14.P3.11.12032818-1
Note:
LON 95w B-230-95-4 E27; 305003311; pcs; 30800; order; 322233.1; KP14278
DSh 60w DSh-230-60 E14; S0100325; pcs; 576; order; 322233.1; KP14278
DSh 40w DSh-230-40 E14; 321600316; pcs; 576; order ;; KP14278
LON 60w B-230-60-4 E27; 303456500; pcs; 16940; order ;; KP14278

The expected out should be in this JSON format

Basic Structure which is expected

{
  "orders": {
    "location": "",
    "deliveryDate": "",
    "orderNo": "",
    "Notes": ""
  },
  "ordersItems": {
    "itemsDetails": [
      {
        "materialdetails": "LON 95w B-230-95-4 E27",
        "referenceNo": "305003311",
        "uom": " pcs",
        "qty": " 30800",
        "docType": " order",
        "projectCode": " 322233.1",
        "description": " KP14278"
      },
      .....
      ...... 
    ]
  }
}

I concur with @aled, this does not look like a proper CSV file, but if you only want to parse the data starting from line 5 onwards you can do something like this:

%dw 2.0
output application/json
var data = "ETM, St. Petersburg
Date: 03/28/12
Number: 14.P3.11.12032818-1
Note:
LON 95w B-230-95-4 E27; 305003311; pcs; 30800; order; 322233.1; KP14278
DSh 60w DSh-230-60 E14; S0100325; pcs; 576; order; 322233.1; KP14278
DSh 40w DSh-230-40 E14; 321600316; pcs; 576; order ;; KP14278
LON 60w B-230-60-4 E27; 303456500; pcs; 16940; order ;; KP14278"

var fs = {
    column_0: "materialDetails",
    column_1: "referenceNo",
    column_2: "uom",
    column_3: "qty",
    column_4: "docType",
    column_5: "projectCode",
    column_6: "description"
}

var parsedRows = read(
    data, 
    "application/csv", 
    {separator: ";",header: false,bodyStartLineNumber: 5}
)

---
{
    orders: {
        location: "",
        deliveryDate: "",
        orderNo: "",
        Notes: ""
    },
    ordersItems: {
        itemsDetails: parsedRows map ($ mapObject {(fs[$$]): $})
    }
}

Note, I setup the data inside a string (primarily because I want my code self-contained) and the parsed using the read() function. You can do something similar and skip the extra lines with your file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM