将多个JSON对象转换为一个列表Mule dataweave

时间:2018-03-17 03:28:01

标签: json mule anypoint-studio dataweave

我输入如下



{
  "ResidentialAddress": {
"type": "RES",
"Building": "String"
  },
  "Name": "Satheesh",
  "Status": "Active",
  "OfficeAddress": {
"type": "OFC",
"Building": "String"
  },
  "TempAddress": {
"type": "TEMP",
"Building": "String"
  }
}




我希望将其转换为以下



{Address:[
{
 "type": "RES",
 "Building": "String"
 },
 {
  "type": "OFC",
  "Building": "String"
  },{
   "type": "TEMP",
   "Building": "String"
   }


]}




当我尝试使用地址:payload.ResidentialAddress ++ payload.TempAddress它给我组合字段而不是列表可以帮助人吗?

3 个答案:

答案 0 :(得分:0)

在dataweave中使用展平操作。在dataweave设置有效载荷之后。

     %dw 1.0
 %output application/json
 ---
(flatten payload) filter ($.type != null)

最终的xml文件将是

<dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[ %dw 1.0
 %output application/json
 ---
 (flatten payload) filter ($.type != null)]]></dw:set-payload>
        </dw:transform-message>
        <set-payload value="{&quot;Address&quot;: #[payload]}" mimeType="application/json" doc:name="Set Payload"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>

<强>输出: enter image description here

答案 1 :(得分:0)

这应该用于过滤具有字符串/整数/布尔值的任何字段:

%dw 1.0
%output application/json
---

{
 Address: (flatten payload) filter ($ is :object)
}

答案 2 :(得分:0)

反对您的测试数据:

%dw 1.0
%output application/json
---
{ Address:payload filter $ is :object map $}

给出:

 {
  "Address": [
    {
      "type": "RES",
      "Building": "String"
    },
    {
      "type": "OFC",
      "Building": "String"
    },
    {
      "type": "TEMP",
      "Building": "String"
    }
  ]
}

但您可能需要调整过滤器以使用您的真实数据......