0

特定の形式で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
    }
)}
}
}

すべてをカバーできたと思います。これがスタックオーバーフローに関する私の最初の投稿であるため、まだ行っていない場合はお知らせください。

4

1 に答える 1

0

Ingredientsなしで他にない場合は、次のSectionことができます。

%dw 1.0
%output application/json
%var addPosition = (list) -> list map {
  "position": $$+1,
  "description" : $
}
---
recipe: ingredients: {(
  ("Ungrouped" + payload.Recipe.*Ingredients.@Section) map {
    "$" : {
      "position": $$,
      "list": addPosition(payload.Recipe.*Ingredients[$$].*Ingredient)
    }
  }
)}
于 2016-01-20T14:38:36.647 に答える