【问题标题】:mule code to add list in dataweave mule在数据编织骡子中添加列表的骡子代码
【发布时间】:2018-02-08 15:21:47
【问题描述】:

我正在尝试从 JSON 文件中提取物理地址和邮寄地址。我可以单独检索它们,但无法将它们合并到dataweave 中。下面的数据编织代码将提供物理地址和邮寄地址列表,但我想要一个包含物理地址和邮寄地址的地址列表。

下面是使用的dataweave代码:

    physicaladdress: payload01.attributes.PhysicalAddress map ((physicalAddress , indexOfPhysicalAddress) -> {
type: physicalAddress.value.AddressType.value ,
lineOne: physicalAddress.value.AddressLine1[0].value as :string ,
lineTwo: physicalAddress.value.AddressLine2[0].value as :string,
country: physicalAddress.value.Country[0].value as :string 
}) when payload01.attributes.PhysicalAddress != null  otherwise null,
    mailingaddress: payload01.attributes.MailingAddress map ((mailingAddress , indexOfMailingAddress) -> {
type: mailingAddress.value.AddressType.value ,
lineOne: mailingAddress.value.AddressLine1[0].value as :string,
lineTwo: mailingAddress.value.AddressLine2[0].value as :string ,
country: mailingAddress.value.Country[0].value as :string 
}) when payload01.attributes.MailingAddress != null otherwise null,

非常感谢任何帮助...

使用附加的数据编织代码的当前响应:

    "physicaladdress": [
  {
    "type": "Physical",
    "addressLine1": "1166 Quail Ct310",
    "cityName": "Pewaukee",
    "country": "United States"
  },
  {
    "type": "Physical",
    "addressLine1": "1166 Quail Ct Ste 310-310 Ste 310",
    "cityName": "Pewaukee",
    "country": "United States"
  }
],
"mailingaddress": [
  {
    "type": "Mailing",
    "lineOne": "1166 Quail Ct Ste 310-310 Ste 310",
    "cityName": "Pewaukee",
    "country": "United States"
  },
  {
    "type": "Mailing",
    "lineOne": "1166 Quail Ct Ste 310",
    "cityName": "Pewaukee",
    "country": "United States"
  }
]

预期响应:

    "address": [
  {
    "type": "Physical",
    "addressLine1": "1166 Quail Ct Ste 310",
    "cityName": "Pewaukee",
    "country": "United States"
  },
  {
    "type": "Physical",
    "addressLine1": "1166 Quail Ct Ste 310-310 Ste 310",
    "cityName": "Pewaukee",
    "country": "United States"
  },
{
    "type": "Mailing",
    "lineOne": "1166 Quail Ct Ste 310-310 Ste 310",
    "cityName": "Pewaukee",
    "country": "United States"
  },
  {
    "type": "Mailing",
    "lineOne": "1166 Quail Ct Ste 310",
    "cityName": "Pewaukee",
    "country": "United States"
  }
]

如果我将 dataweave 代码更改为下面的代码,我将只获得邮寄地址,认为邮寄地址会覆盖物理地址:

    address: payload01.attributes.PhysicalAddress map ((physicalAddress , indexOfPhysicalAddress) -> {
type: physicalAddress.value.AddressType.value ,
lineOne: physicalAddress.value.AddressLine1[0].value as :string ,
lineTwo: physicalAddress.value.AddressLine2[0].value as :string,
country: physicalAddress.value.Country[0].value as :string 
}) when payload01.attributes.PhysicalAddress != null  otherwise null,
    address: payload01.attributes.MailingAddress map ((mailingAddress , indexOfMailingAddress) -> {
type: mailingAddress.value.AddressType.value ,
lineOne: mailingAddress.value.AddressLine1[0].value as :string,
lineTwo: mailingAddress.value.AddressLine2[0].value as :string ,
country: mailingAddress.value.Country[0].value as :string 
}) when payload01.attributes.MailingAddress != null otherwise null,

我正在寻找附有列表(物理和邮寄)示例的地址,以供参考。

【问题讨论】:

    标签: json merge mule mule-component dataweave


    【解决方案1】:

    看看这对你有没有帮助:

    Sample Input :
    
    {
    
    "physicaladdressList" : [
                "physicaladdress" : {
                    "type":  "01" ,
                    "lineOne": " 1st Address Line1(P)",
                    "lineTwo": " 1st Address Line2 (P)",
                    "country": "INDIA" 
                 },
             "physicaladdress" :{
                "type":  "02" ,
                "lineOne": "2nd Address Line1(P)",
                "lineTwo": "2nd Address Line2(P)",
                "country": "INDIA" 
             },
              "physicaladdress" : {
                "type":  "03" ,
                "lineOne": "3rd Address Line1(P)",
                "lineTwo": "3rd Address Line2(P)",
                "country": "INDIA" 
             }
     ],
     "mailingaddressList" : [
              "mailingaddress" :{
                    "type":  "04" ,
                    "lineOne": "1st Address Line1(M)",
                    "lineTwo": "1st Address Line2(M)",
                    "country": "INDIA" 
                 },
             "mailingaddress" : {
                "type":  "05" ,
                "lineOne": "2nd Address Line1(M)",
                "lineTwo": "2nd Address Line2(M)",
                "country": "INDIA" 
             },
             "mailingaddress" : {
                "type":  "06" ,
                "lineOne": "3rd Address Line1(M)",
                "lineTwo": "3rd Address Line2(M)",
                "country": "INDIA" 
             }]
    
     }
    
    ===============DataWeave===============================
    %dw 1.0
    %input payload application/json
    %output application/json
    %var phydata = payload.physicaladdressList.physicaladdress 
    %var mailddata = payload.mailingaddressList.mailingaddress 
    %var b = sizeOf (payload.physicaladdressList.physicaladdress)
    %var a = 0
    %var c= b - 1
    ---
       [[a][0] .. [c][0]] map {
            Address :
          {
              PhysicalAddress: 
                { 
                    "Type" : phydata[$][0].type,
                    "AddressLine1" : phydata[$][1].lineOne,
                    "AddressLine2" : phydata[$][2].lineTwo,
                    "Country" : phydata[$][3].country
                },
                MailingAddress: 
                { 
                    "Type" : mailddata[$][0].type,
                    "AddressLine1" : mailddata[$][1].lineOne,
                    "AddressLine2" : mailddata[$][2].lineTwo,
                    "Country" : mailddata[$][3].country
                }
    
           }             
              }
    
    ==========OutPut============================
    [
      {
        "Address": {
          "PhysicalAddress": {
            "Type": "01",
            "AddressLine1": " 1st Address Line1(P)",
            "AddressLine2": " 1st Address Line2 (P)",
            "Country": "INDIA"
          },
          "MailingAddress": {
            "Type": "04",
            "AddressLine1": "1st Address Line1(M)",
            "AddressLine2": "1st Address Line2(M)",
            "Country": "INDIA"
          }
        }
      },
      {
        "Address": {
          "PhysicalAddress": {
            "Type": "02",
            "AddressLine1": "2nd Address Line1(P)",
            "AddressLine2": "2nd Address Line2(P)",
            "Country": "INDIA"
          },
          "MailingAddress": {
            "Type": "05",
            "AddressLine1": "2nd Address Line1(M)",
            "AddressLine2": "2nd Address Line2(M)",
            "Country": "INDIA"
          }
        }
      },
      {
        "Address": {
          "PhysicalAddress": {
            "Type": "03",
            "AddressLine1": "3rd Address Line1(P)",
            "AddressLine2": "3rd Address Line2(P)",
            "Country": "INDIA"
          },
          "MailingAddress": {
            "Type": "06",
            "AddressLine1": "3rd Address Line1(M)",
            "AddressLine2": "3rd Address Line2(M)",
            "Country": "INDIA"
          }
        }
      }
    ]
    

    【讨论】:

    • 感谢 Mowmita 抽出宝贵时间。我已附上当前输出和预期输出,以提供有关我遇到的场景的更多信息。
    • @chethan : 使用以下代码 : %dw 1.0 %input payload application/json %output application/json %var mailddata = payload.mailingaddress --- "Address" : payload.physicaladdress map { " type" : $.type, "addressLine1" : $.addressLine1, "cityName" : $.cityName, "country" : $.country } ++ mailddata
    【解决方案2】:

    输入:

    {
        "Address": {
          "PhysicalAddress": {
            "Type": "01",
            "AddressLine1": " PhysicalAddress Address Line1(P)",
            "AddressLine2": " PhysicalAddress Address Line2 (P)",
            "Country": "INDIA"
          },
          "MailingAddress": {
            "Type": "04",
            "AddressLine1": "MailingAddress Address Line1(M)",
            "AddressLine2": "MailingAddress Address Line2(M)",
            "Country": "INDIA"
          }
        }
      }
    

    数据编织

    %dw 1.0
    %output application/json
    ---
    
    
    {Address : "MailingAddress": payload.Address.MailingAddress.AddressLine1
    +"PhysicalAddress":payload.Address.PhysicalAddress.AddressLine1,
    Address2 : "MailingAddress": payload.Address.MailingAddress.AddressLine2
    +"PhysicalAddress":payload.Address.PhysicalAddress.AddressLine2}
    

    输出

    {"Address": {
    "MailingAddress": [
      "MailingAddress Address Line1(M)",
      " PhysicalAddress Address Line1(P)"
    ]  "Address2": {
    "MailingAddress": [
      "MailingAddress Address Line2(M)",
      " PhysicalAddress Address Line2 (P)"
    ]}}}
    

    【讨论】:

    • 感谢 jitesh 抽出宝贵时间。我应该提供示例来解释这种情况。我已使用当前输出和预期输出更新了查询,以提供有关该场景的更多信息。非常感谢您的帮助。
    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    相关资源
    最近更新 更多