我正在嘗試使用dataweave以特定格式創建json。Dataweave XML to JSON根據xml中的屬性創建密鑰對值
xml在某些元素中有屬性,我需要用它來在json文件中創建一個未定義的鍵。
XML
<Ingredients>
<Ingredient>225g/8oz unsalted butter, softened, plus extra for greasing</Ingredient>
<Ingredient>175g/6oz dried cranberries</Ingredient>
</Ingredients>
<Ingredients Section="FOR THE FRUIT">
<Ingredient>150ml/¼pt cloudy apple juice</Ingredient>
<Ingredient>50g/2oz unsalted butter</Ingredient>
</Ingredients>
<Ingredients Section="TO FEED THE CAKE (each time)">
<Ingredient>2 tbsp dark rum</Ingredient>
<Ingredient>1 tbsp maple syrup</Ingredient>
</Ingredients>
JSON的輸出應該是這樣的。訣竅是當Section = null時,應該使用Ungrouped,否則使用Section的值。
JSON
{
"ingredients": {
"Ungrouped": {
"position": 0,
"list": [{
"position": 1,
"description": "225g/8oz unsalted butter, softened, plus extra for greasing"
}, {
"position": 2,
"description": "225g/8oz light muscovado sugar"
}]
},
"FOR THE FRUIT": {
"position": 1,
"list": [{
"position": 1,
"description": "150ml/¼pt cloudy apple juice"
}, {
"position": 2,
"description": "50g/2oz unsalted butter"
}]
},
"TO FEED THE CAKE (each time)":{
"position":2,
"list": [{
"position": 1,
"description": "2 tbsp dark rum"
},
{
"position": 2,
"description": "1 tbsp maple syrup"
}]
}
}
}
這裏是我的數據編織的開始。我還沒有取得進一步的進展,因爲我能夠設置未分組部分至關重要。
Dataweave
%dw 1.0
%input payload application/xml
%output application/json
---
{
recipe: {
"ingredients": { (payload.Recipe.*[email protected] map
'Ungrouped':{
position : $$+0
} when $ == null otherwise
'$':{
position : $$+0
}
)}
}
}
我希望我已經涵蓋了一切。請讓我知道,如果我沒有,因爲這是我在stackoverflow上的第一篇文章。
所以,困難的部分是把所有具有無節,右邊的成分?你可以有更多的配料標籤沒有部分? – Shoki