下面的动态响应来自第三方API,现在我只需要将特定的JSON对象("MyValues")转换为数组。
这里的有效负载是样本是非常大的。
当前输出:
{
"Body": {
"Status": "200",
"Result": {
"MyValues":{
"Name":"ABC TEST",
"Phone":"1234"
}
}
}
}
预期输出:
{
"Body": {
"Status": "200",
"Result": {
"MyValues":[{
"Name":"ABC TEST",
"Phone":"1234"
}]
}
}
}
发布于 2021-04-04 19:50:21
您可以根据接收到的类型、数组或对象使用模式匹配。我创建了一个递归函数来查找键名的实例,并以通用的方式执行更改。
示例:
%dw 1.0
%output application/json
%function convertToSingleArray(x, key)
x match {
// OPTIONAL :array -> x map convertToSingleArray($, key),
:object -> x mapObject {($$): [$] when ( (($$ as :string) == key) and ((typeOf $) as :string == ":object")) otherwise convertToSingleArray($, key)
},
default -> x
}
---
convertToSingleArray(payload, "MyValues")
https://stackoverflow.com/questions/66939363
复制相似问题