如何将应在 <space> 上拆分的字符串转换为 mulesoft 中的对象数组

How to turn a string which should be split on the <space> an turned into an array of objects in mulesoft

我想把一个应该在space的基础上拆分的字符串变成一个对象数组。 请帮助我如何形成它。

输入

field: YYC:16:26 YVR:16:03 YEG:13:43

预期输出

"details" : [     
  {
    "field" : "YYC",         
    "time" : "16:26"     
   },  
   {
      "field" : "YVR",
      "Time" : "16:03"     
    }, 
    {
      "field" : "YEG",
      "Time" : "13:43"     
    }
]

您需要先按空格拆分,然后按如下方式拆分剩余的字符串

%dw 2.0
output application/json
var test= "YYC:16:26 YVR:16:03 YEG:13:43" splitBy  " "
---
details: test map ((item, index) -> 
{
    "field": item[0 to 2],
    "Time": item [4 to -1]
})


另一种类似于 Anurag 解决方案的方法

DW

%dw 2.0
output application/json
var test= "YYC:16:26 YVR:16:03 YEG:13:43" splitBy  " "
---
details: test map ((item, index) -> 
{
    "field": (item splitBy ":")[0],
    "Time": (item splitBy ":")[1 to -1] joinBy ":"
})

输出

{
  "details": [
    {
      "field": "YYC",
      "Time": "16:26"
    },
    {
      "field": "YVR",
      "Time": "16:03"
    },
    {
      "field": "YEG",
      "Time": "13:43"
    }
  ]
}

对 Karthik 发布的内容略有改动:

%dw 2.0
output application/json
import * from dw::core::Arrays
var test= "YYC:16:26 YVR:16:03 YEG:13:43" splitBy  " "
---
details: test map 
{
    "field": ($ splitBy ":")[0],
    "Time":  drop(($ splitBy ":"),1)joinBy ":"
}