如何使用 dataweave 2.0

问题描述

我需要解析这样一个 API 的响应:

"[{\"Customers\":[{\"Id\":1607715391563}],\"Partners\":[],\"ModDate\":\"\\/Date(1608031919597)\\/\",\"CreatedByUserId\":null},{\"Message\":null,\"Code\":\"200\",\"NextPage\":1}]"

我希望我有这样的:

[
   {
      "Customers":[
         {
            "Id":1607715391563
         }
      ],"Partners":[
         
      ],"ModDate":"/Date(1608031919597)/","CreatedByUserId":null
   },{
      "Message":null,"Code":"200","NextPage":1
   }
]

我已经尝试使用 payload[1 to -2] 删除字符串,并使用 read(payload[1 to -2],'application/json') 解析 JSON。我已经尝试遵循此 link 的一些提示,但都没有奏效。

编辑: 这里的重点是我想访问,例如,其他连接器中的Customers.Id值,我不能

解决方法

这个怎么样?

enter image description here

%dw 2.0
output application/json
var inpString = "[{\"Customers\":[{\"Id\":1607715391563}],\"Partners\":[],\"ModDate\":\"\\/Date(1608031919597)\\/\",\"CreatedByUserId\":null},{\"Message\":null,\"Code\":\"200\",\"NextPage\":1}]"
---
read(inpString,"application/json")
,

您可以尝试以下 DataWeave 表达式:

%dw 2.0
output application/json
---
read((payload replace /^"|"$/ with '') replace '\"' with '"',"application/json")

第一个替换将删除标题和尾随双引号,第二个替换将用双引号替换反斜杠转义双引号。