【问题标题】:How to Convert CSV to JSON in Dataweave 2.0 in mulesoft without specific headers?如何在没有特定标头的 mulesoft 中的 Dataweave 2.0 中将 CSV 转换为 JSON?
【发布时间】:2020-07-31 05:52:07
【问题描述】:

我有没有标题的 CSV 输入文件

输入示例如下

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

预期的输出应该是这种 JSON 格式

预期的基本结构

{
  "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"
      },
      .....
      ...... 
    ]
  }
}

【问题讨论】:

  • 输入不是 CSV。只有Note: 下面的数据可以被视为 CSV。

标签: mule dataweave mulesoft


【解决方案1】:

我同意@aled,这看起来不像一个正确的 CSV 文件,但如果您只想解析从第 5 行开始的数据,您可以执行以下操作:

%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[$$]): $})
    }
}

注意,我将数据设置在一个字符串中(主要是因为我希望我的代码是独立的)并使用read() 函数进行解析。您可以执行类似的操作并跳过文件中的额外行。

【讨论】:

  • 这很整洁。我忘记了 CSV 的 DataWeave bodyStartLineNumber 读取器属性。
  • 谢谢。实际上 CSV 格式是俄语,所以我通过翻译器翻译了它,所以格式可能不匹配。我还想获得高于起始行号的分支和日期的值。我有一个查询,比如它是否适用于动态的大量记录,比如从文件夹中获取它。?
  • этм,спбдата:28.03.12。 60期ш-230-60 e14; Q.0100325;ш; 576;зз讯; 322233.1; 322233.1;见14278 40в40в-230--230-230дimш230时间; 321600316; 321600316; 32160031; 306; 576; 576; 576; ;шт;16940;заказ;;КП14278
  • 修改原始帖子以包含俄语输入以及您需要的确切输出。
猜你喜欢
  • 1970-01-01
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
  • 2018-08-01
  • 2021-11-11
  • 1970-01-01
相关资源
最近更新 更多