特定の形式でjsonを作成するためにdataweaveを使用しようとしています。
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"
}]
}
}
}
これが私のデータ織りの始まりです。グループ化されていないセクションを設定できることが重要であるため、これ以上進んでいません。
データウィーブ
%dw 1.0
%input payload application/xml
%output application/json
---
{
recipe: {
"ingredients": { (payload.Recipe.*Ingredients.@Section map
'Ungrouped':{
position : $$+0
} when $ == null otherwise
'$':{
position : $$+0
}
)}
}
}
すべてをカバーできたと思います。これがスタックオーバーフローに関する私の最初の投稿であるため、まだ行っていない場合はお知らせください。