如何使用 dataweave 2.0 解析带有转义 json 的字符串
how to parse a string with escaped json using 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 值,但我不能
您可以试试下面的DataWeave表达式:
%dw 2.0
output application/json
---
read((payload replace /^"|"$/ with '') replace '\"' with '"', "application/json")
第一个替换将删除标题和尾随双引号,第二个替换将用双引号替换反斜杠转义双引号。
这个怎么样?
%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")
我需要解析 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 值,但我不能
您可以试试下面的DataWeave表达式:
%dw 2.0
output application/json
---
read((payload replace /^"|"$/ with '') replace '\"' with '"', "application/json")
第一个替换将删除标题和尾随双引号,第二个替换将用双引号替换反斜杠转义双引号。
这个怎么样?
%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")