簡體   English   中英

將多個JSON對象轉換為一個列表Mule dataweave

[英]Converting multiple JSON object to one list Mule 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" } ]} 

當我嘗試使用address:payload.ResidentialAddress ++ payload.TempAddress時,它給了我組合的字段,而不是列表,誰能幫忙?

在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"/>

輸出: 在此處輸入圖片說明

這應該適用於過濾具有字符串/整數/布爾值的任何字段:

%dw 1.0
%output application/json
---

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

針對您的測試數據:

%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"
    }
  ]
}

但您可能需要調整過濾條件才能使用真實數據...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM