在 mule 中读取文件并使用 dataweave 进行解析

发布于 2025-01-18 04:08:07 字数 709 浏览 3 评论 0原文

我正在使用m子4.4,并尝试读取文件,然后使用DataWeave转换为JSON。 这是文件(没有标题),而

abc|Milk|3.9|
lmn|Butter|5.5|
xyz|Bread|1.6|

在读取文件将MIME类型设置为应用程序/CSV时,文件中的最后一行是空白的(尽管其管道界定),这会是问题吗?

我遇到的问题是,我想将文件内容转换为以下的JSON:

[
 {
   "id": "abc",
   "product": "Milk",
   "price": 3.9
 },
{
"id": "lmn",
   "product": "Butter",
   "price": 5.5
 },
etc

]

但是它显示如下:(第一行重复)

[
 {
   "id": {
     "abc|Milk|3.9|": "lmn|Butter|5.5|"
    }
 },
{
"id": {
  "abc|Milk|3.9|": "xyz|Bread|1.6|"
}
}
]

我认为这是在发生这种情况,因为Mule假设第一行要包含标头。 这是我的dataweave:

%dw 2.0
output application/json

---

payload map (value,index)->
{
   id:value
}

I am using Mule 4.4 and am trying to read a file and then convert into JSON using Dataweave.
Here is the file ( it has no headers ) and last line in file is blank

abc|Milk|3.9|
lmn|Butter|5.5|
xyz|Bread|1.6|

while reading the file have set mime type as application/csv ( though its pipe delimited ) , will that be a problem ?

The problem I am encountering is , I want to transform the file content into json like below:

[
 {
   "id": "abc",
   "product": "Milk",
   "price": 3.9
 },
{
"id": "lmn",
   "product": "Butter",
   "price": 5.5
 },
etc

]

However it is showing up like below : ( with the first row repeating )

[
 {
   "id": {
     "abc|Milk|3.9|": "lmn|Butter|5.5|"
    }
 },
{
"id": {
  "abc|Milk|3.9|": "xyz|Bread|1.6|"
}
}
]

I think this is happening since mule is assuming first row to contain a header .
Here is my dataweave :

%dw 2.0
output application/json

---

payload map (value,index)->
{
   id:value
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜深人未静 2025-01-25 04:08:07

它以这种方式显示是因为您将其设置为将第一行视为标题行。您需要将阅读器的 header 值设置为 false,以便它了解第一行实际上不是标题。

除此之外,您还需要将分隔符设置为管道字符。

例如,如果您将其作为文本读取并纯粹使用 dataweave 对其进行处理,则它将如下所示:

%dw 2.0
output application/json
---
read(payload, "applicatin/csv", {"header": false, "separator": "|" }) map (
    {
        id: $[0],
        product: $[1],
        price: $[2]
    }
)

在此处输入图像描述

It is showing up that way because you have it set in such a way that it considers the first row to be your header row. You need to set the header value for the reader to false so that it understands that the first row is not, in fact, a header.

In addition to that, you will need to set the separator to the pipe character.

For example, if you read it in as text and processed it purely using dataweave, it would look like:

%dw 2.0
output application/json
---
read(payload, "applicatin/csv", {"header": false, "separator": "|" }) map (
    {
        id: $[0],
        product: $[1],
        price: $[2]
    }
)

enter image description here

美人如玉 2025-01-25 04:08:07

请在下面尝试。

%dw 2.0
input payload application/csv separator="|", header = false
output application/json
---
payload map (value,index)->
{
    id:value[0],
    product: value[1],
    price: value[2]
}

Please try as below.

%dw 2.0
input payload application/csv separator="|", header = false
output application/json
---
payload map (value,index)->
{
    id:value[0],
    product: value[1],
    price: value[2]
}

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文