我具有如下所示的XML结构,可以转换为JSON。
<Root>
<PrimeEntity>
<FinEntity>
<RegData>
<CustValue>Test</CustValue>
</RegData>
<PassEntity>
<PassInnerEntity>
<Desc>DataTest1</Desc>
</PassInnerEntity>
</PassEntity>
<PassInnerEntity>
<Desc>DataTest2</Desc>
</PassInnerEntity>
<PassEntity>
<PassInnerEntity>
<Desc>DataTest3</Desc>
</PassInnerEntity>
</PassEntity>
<PassEntity>
<PassInnerEntity>
</PassInnerEntity>
</PassEntity>
</FinEntity>
</PrimeEntity></Root>
数据应转换为以下JSON:
"Transactions": [
{
"Id": "Test-DataTest1-DataTest2-DataTest3"
}]
我正在为这种映射而苦苦挣扎,需要一些帮助。
我尝试使用@Jerney的答案,但我需要更多帮助。
更多信息:我在Mule中使用For-each处理器来迭代“ PrimeEntity”,然后将结果汇总。使用内部For-Each处理器DWL映射到“ FinEntity”,并且在此“ ID”详细信息内需要。当我尝试映射'PassEntity'时,我能够获得所需的结果,但是我无法连接'CustValue'的值,因为无法在字符串和数组之间使用'++'进行串联。相同的代码段是:
($.*PassEntity map
{Description: ($.*PassInnerEntity map {
data: $.Desc
})
})
XML可能具有重复的复杂元素,例如“ FinEntity”和“ PassEntity”,而“ RegData”将只是其中之一。我在下面提供请求xml:
<Root>
<PrimeEntity>
<FinEntity>
<RegData>
Tag does not exists
</RegData>
<PassEntity>
<PassInnerEntity>
<Desc>DataTest1</Desc>
</PassInnerEntity>
</PassEntity>
<PassInnerEntity>
<Desc>DataTest2</Desc>
</PassInnerEntity>
<PassInnerEntity>
<Desc>DataTest22</Desc>
</PassInnerEntity>
<PassEntity>
<PassInnerEntity>
<Desc>DataTest3</Desc>
</PassInnerEntity>
</PassEntity>
<PassEntity>
<PassInnerEntity></PassInnerEntity>
</PassEntity>
</FinEntity>
<FinEntity>
<RegData>
<CustValue>Test2</CustValue>
</RegData>
<PassEntity>
<PassInnerEntity>
<Desc>DataTest12</Desc>
</PassInnerEntity>
</PassEntity>
<PassInnerEntity>
<Desc>DataTest22</Desc>
</PassInnerEntity>
<PassInnerEntity>
<Desc>DataTest23</Desc>
</PassInnerEntity>
<PassEntity>
<PassInnerEntity>
<Desc>DataTest24</Desc>
</PassInnerEntity>
</PassEntity>
<PassEntity>
<PassInnerEntity></PassInnerEntity>
</PassEntity>
</FinEntity>
</PrimeEntity>
<PrimeEntity>
<FinEntity>
<RegData>
<CustValue>Test3</CustValue>
</RegData>
<PassEntity>
<PassInnerEntity>
<Desc>DataTest31</Desc>
</PassInnerEntity>
</PassEntity>
<PassInnerEntity>
<Desc>DataTest32</Desc>
</PassInnerEntity>
<PassInnerEntity>
<Desc>DataTest33</Desc>
</PassInnerEntity>
<PassEntity>
<PassInnerEntity>
<Desc>DataTest34</Desc>
</PassInnerEntity>
</PassEntity>
<PassEntity>
<PassInnerEntity></PassInnerEntity>
</PassEntity>
</FinEntity>
<FinEntity>
<RegData>
<CustValue>Test4</CustValue>
</RegData>
<PassEntity>
<PassInnerEntity>
<Desc>DataTest41</Desc>
</PassInnerEntity>
</PassEntity>
<PassInnerEntity>
<Desc>DataTest42</Desc>
</PassInnerEntity>
<PassInnerEntity>
<Desc>DataTest43</Desc>
</PassInnerEntity>
<PassEntity>
<PassInnerEntity>
<Desc>DataTest44</Desc>
</PassInnerEntity>
</PassEntity>
<PassEntity>
<PassInnerEntity></PassInnerEntity>
</PassEntity>
</FinEntity>
</PrimeEntity>
</Root>
具有实际和预期详细信息的JSON结果:
"Transactions": [
{
"Id": ""
},
{
"Id": "" -- this is coming as null but this is required. Required String
"Test2-DataTest12-DataTest22-DataTest23-DataTest24"
},
{
"Id": "Test3-DataTest31-DataTest32-DataTest33-DataTest34-DataTest41- DataTest42-DataTest43-DataTest44" -- this should be "Test3-DataTest31- DataTest32-DataTest33-DataTest34"
},
{
"Id": "Test4-DataTest31-DataTest32-DataTest33-DataTest34-DataTest41-`DataTest42-DataTest43-DataTest44" -- this should be "Test3-DataTest41-
DataTest42-DataTest43-DataTest44"
}]`
答案 0 :(得分:0)
您将要使用后代选择器..
来获取所有Desc
元素。这将返回一个数组。使用joinBy
将其放入您想要的字符串中:
%dw 1.0
%output application/json
%var custValue = payload.Root.PrimeEntity.FinEntity.RegData.CustValue
%var descs = payload..Desc joinBy "-"
---
{
Transactions: [
[
"Id": custValue ++ '-' ++ descs
]
]
}
输出为:
{
"Transactions": [
[
{
"Id": "Test-DataTest1-DataTest2-DataTest3"
}
]
]
}
答案 1 :(得分:0)
遵循dataweave代码应该可以正常工作。
%dw 1.0
%output application/json
---
Transactions : payload.Root.PrimeEntity.*FinEntity map {
Id : ($..CustValue ++ $..Desc) joinBy "-"
}
希望这会有所帮助。
答案 2 :(得分:0)
我们开始吧,这将产生预期的输出
%dw 2.0
output application/json
---
Transactions :(PrimeEntity : payload.Root.*PrimeEntity map (primeData,indexOfPrimeData) -> {
FinEntity : primeData.*FinEntity map (FinData,indexOfFinData) -> {
idData : (if(typeOf(FinData.RegData) != String){
id : FinData.RegData.CustValue ++ '-' ++ (FinData..Desc joinBy '-')
}
else {
id : ""
})
}
})..idData