简体   繁体   English

在 Mule 中使用 Dataweave 将分隔符填充转换为 JSON 格式

[英]Convert Delimiter fille to JSON format using Dataweave in Mule

I need to convert existing delimiter file to json format using dataweave in mule.我需要在 mule 中使用 dataweave 将现有的分隔符文件转换为 json 格式。

Sample Input:样本输入:

Name~#~ID~#~Company~#~Address
SRI~#~1~#~Infy~#~Bangalore
Rahul~#~2~#IBM~#~US
John~#~3~#~SF~#~UK

Sample outPut样品输出

{
Name: Sri
ID: 1
Company: Infy
Adress: Bangalore
},
{
Name: Rahul
ID: 2
Company: IBM
Adress: US

},
{
Name: John
ID: 3
Company: SF
Adress: UK
}

But i am getting below output但我低于输出

输出

Dataweave Transforamtion数据编织转换

数据编织转换

Very interesting question you can use readLines function from dw::core::Binaries and then split by ~#~ .非常有趣的问题,您可以使用dw::core::Binaries readLines函数,然后按~#~拆分。 Remember to set your payload to application/octet-stream mimeType so dw will handle this as a Binary data and later you can use this snippet to parse it.请记住将您的有效负载设置为application/octet-stream mimeType,以便 dw 将其作为二进制数据处理,稍后您可以使用此代码段来解析它。

%dw 2.0
output application/json
import dw::core::Binaries
var lines = Binaries::readLinesWith(payload, "UTF-8")


---
lines match {
    case [x ~ xs] -> do {
        var header = x splitBy  "~#~"
        ---
        xs map ((item, index) -> {
              (item splitBy "~#~" map (column, index) -> {
                  (header[index]): column
              } )
        })
    }
}

With the following input (input.txt):使用以下输入(input.txt):

Name~#~ID~#~Company~#~Address
SRI~#~1~#~Infy~#~Bangalore
Rahul~#~2~#~IBM~#~US
John~#~3~#~SF~#~UK

And the following DataWeave code:以及以下 DataWeave 代码:

%dw 2.0
output application/json

var payload = readUrl("classpath://input.txt","text/plain") splitBy(/\r\n/)

var header= payload[0] splitBy(/~#~/)
var data  = payload[1 to -1]
---
data map (item, index) ->
    {(item splitBy(/~#~/) map
    {   
        (header[$$]): $ 
    })}

The result is:结果是:

[
  {
    "Name": "SRI",
    "ID": "1",
    "Company": "Infy",
    "Address": "Bangalore"
  },
  {
    "Name": "Rahul",
    "ID": "2",
    "Company": "IBM",
    "Address": "US"
  },
  {
    "Name": "John",
    "ID": "3",
    "Company": "SF",
    "Address": "UK"
  }
]

I recommend an array as an output我推荐一个数组作为输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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