簡體   English   中英

在m子中的dataweave中將Json轉換為xml

[英]Convert Json to xml in dataweave in mule

我正在使用數據編織將Json轉換為XML ..需要幫助..

我的輸入json:

如果json值是這樣的

 {"whiteaudience": [
   {
    "audienceType": {
      "Id": "70000",
      "Name": "whiteau"

    }
  },
  {
    "audienceType": {
      "Id": "70000",
      "Name": "whiteau"

    }
  }
],
"blackaudience": [
  {
    "audienceType": {
      "Id": "",
      "Name": ""

    }
  },
  {
    "audienceType": {
      "Id": "",
      "Name": ""

    }
  }
]
}

thnan output XML be like 

<ColuredAudience>
    <whiteaudience>
        <item>70000</item>
        <item>70000</item>
    </whiteaudience>
</ColuredAudience>


and if input json is like this 

{"whiteaudience": [
  {
    "audienceType": {
      "Id": "",
      "Name": ""

    }
  },
  {
    "audienceType": {
      "Id": "",
      "Name": ""

    }
  }
],
"blackaudience": [
  {
    "audienceType": {
      "Id": "80000",
      "Name": " blackau"

    }
  },
  {
    "audienceType": {
      "Id": "80000",
      "Name": "blackau"

    }
  }
]
}
thnan output XML be like 

<ColuredAudience>
    <blackaudience>
        <item>80000</item>
        <item>80000</item>
    </blackaudience>
</ColuredAudience>


So the logic is ,i will get value(s) either for whiteaudience.id or  blackaudience.id  so if the values is coming for whiteaudience than blackauidence tag will come with empty tag(s) and viceversa..

所以首先,我必須檢查哪些受眾群體標簽具有價值,而不是如果價值來自blackauidence,那么只有黑受眾會來,如果價值來自whiteaudience,那么whiteaudience標簽會來

Please advice how to do mapping and use filter in such senarios

Cheers,
Bolver

看一下這個:

%dw 1.0 
%output application/xml
---
{
    "ColuredAudience": {
        "include":{
            (payload.whiteaudience filter ($.audienceType.Id !="") map (
                "item": $.audienceType.Id
            )),
            (payload.blackaudience filter ($.audienceType.Id !="") map (
                "item": $.audienceType.Id
            ))
        }
    }
}

根據您的要求,您可以使用Dataweave中的if條件 跳過具有""值的元素,並輕松獲得預期結果:-

   %dw 1.0
   %output application/xml 
   ---
   {
        ColuredAudience: {
          include: { 
              (payload.whiteaudience map 
              {(item: $.audienceType.Id) when $.audienceType.Id !=""}),

              (payload.blackaudience map 
              {(item: $.audienceType.Id) when $.audienceType.Id != ""})
            }
          }
      } 

檢查是否有幫助:

%dw 1.0

%輸出應用程序/ XML

{“” ColuredAudience“:{(”“ blackaudience”:{

        (payload.blackaudience  map (
            "item": $.audienceType.Id
        )

        )
        }) when (payload.blackaudience[0].audienceType.Id !="")
        ,


    ("whiteaudience":{
        (payload.whiteaudience  map (
            "item": $.audienceType.Id
        )
        )
        } )  when (payload.whiteaudience[0].audienceType.Id !="")
    }

    }

暫無
暫無

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

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