2016-05-14 81 views
0

我需要將json(在多次調用後獲得,然後合併它)轉換爲特定結構。Json使用mule dataweave轉換

這裏是我輸入的JSON有效載荷

[{ 
    "shops": [{ 
     "shop": { 
      "code": "AU5", 
      "streetName": "a", 
      "city": "a", 
      "district": "a", 
      "state": "a", 
      "postalCode": "a", 
      "country": "a" 
     } 
    }, { 
     "shop": { 
      "code": "b", 
      "streetName": "b", 
      "city": "b", 
      "district": "b", 
      "state": "b", 
      "postalCode": "b", 
      "country": "b" 
     } 
    }] 
}, 


[ 
    [{ 
     "salesOffice": { 
      "shop": { 
       "code": "AU5" 
      }, 
      "office": "MEL", 
      "branch": "MEL", 
      "district": "SPR", 
      "subRegion": "SPR", 
      "region": "AP" 
     } 
    }], 
    [{ 
      "salesOffice": { 
       "shop": { 
        "code": "b" 
       }, 
       "office": "999", 
       "branch": "999", 
       "district": "999", 
       "subRegion": "999", 
       "region": "999" 
      } 
     } 

    ] 

]] 
以下

的預期輸出JSON

{ 
"shops": [ 
    { 
     "shop": { 
      "code": "AU5", 
      "streetName": "a", 
      "city": "a", 
      "district": "a", 
      "state": "a", 
      "postalCode": "a", 
      "country": "a", 
      "salesOffice": { 
       "office": "MEL", 
       "branch": "MEL", 
       "district": "SPR", 
       "subRegion": "SPR", 
       "region": "AP" 
      } 
     } 
    }, 
    { 
     "shop": { 
      "code": "b", 
      "streetName": "b", 
      "city": "b", 
      "district": "b", 
      "state": "b", 
      "postalCode": "b", 
      "country": "b", 
      "salesOffice": { 
       "office": "999", 
       "branch": "999", 
       "district": "999", 
       "subRegion": "999", 
       "region": "999" 
      } 
     } 
    } 
]} 

改造的同時,店裏面的 '代碼' 應與內部salesOffice '代碼' >>店> >'code'

下面是Json Schema for the output payload(應該對輸出進行驗證)

{ 
"$schema": "http://json-schema.org/draft-04/schema#", 

"type": "object", 
"properties": { 
    "shops": { 

     "type": "array", 
     "items": { 

      "type": "object", 
      "properties": { 
       "shop": { 

        "type": "object", 
        "properties": { 
         "code": { 

          "type": "string" 
         }, 
         "streetName": { 

          "type": "string" 
         }, 
         "city": { 

          "type": "string" 
         }, 
         "district": { 

          "type": "string" 
         }, 
         "state": { 

          "type": "string" 
         }, 
         "postalCode": { 

          "type": "string" 
         }, 
         "country": { 

          "type": "string" 
         } 
        }, 
        "salesOffice": { 

         "type": "object", 
          "properties": { 
           "office": { 

           "type": "string" 
           }, 
           "branch": { 

           "type": "string" 
           }, 
           "district": { 

           "type": "string" 
           }, 
           "subRegion": { 

           "type": "string" 
           }, 
           "region": { 

           "type": "string" 
           } 
          } 
         }, 
        "required": [ 
         "code", 
         "streetName", 
         "city", 
         "district", 
         "state", 
         "postalCode", 
         "country", 
         "salesOffice" 
        ] 
       } 
      }, 
      "required": [ 
       "shop" 
      ] 
     } 
    } 
}, 
"required": [ 
    "shops" 
]} 

任何解決方案或任何指針將是一個很大的幫助

回答

2

請參考下dataweave:

%dw 1.0 
%output application/json 
--- 
{ 
shops: using (salesOffice = payload[1]..salesOffice)(payload[0].shops map { 
shop : 
    { 
    code: $.shop.code, 
    streetName:$.shop.streetName, 
    city:$.shop.city, 
    district:$.shop.district, 
    state:$.shop.state, 
    postalCode:$.shop.postalCode, 
    country:$.shop.country, 
    salesOffice: using (code= $.shop.code) (salesOffice[?(code == $.shop.code)] map { 
    office:$.office, 
    branch:$.branch, 
    district:$.district, 
    subRegion:$.subRegion, 
    region:$.region 
})[0] 
} 
}) 
} 
+0

非常感謝..它工作正常! – Anand

0

您可以使用驗證JSON模式騾子組成部分。