在 mule 中读取文件并使用 dataweave 进行解析
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它以这种方式显示是因为您将其设置为将第一行视为标题行。您需要将阅读器的
header
值设置为 false,以便它了解第一行实际上不是标题。除此之外,您还需要将分隔符设置为管道字符。
例如,如果您将其作为文本读取并纯粹使用 dataweave 对其进行处理,则它将如下所示:
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:
请在下面尝试。
Please try as below.