Flex Gateway新着情報
Governance新着情報
Monitoring API ManagerDataWeave では、DataWeave オブジェクト、配列、またはサブ配列において、配列のサブ配列やキー/値ペアのコレクションをフラット化できます。 開始する前に、Mule 4 アプリケーションでは DataWeave のバージョン 2.x が使用されることに注意してください。Mule 3 アプリケーションでの DataWeave については、『DataWeave バージョン 1.2 の例』を参照してください。 他の DataWeave バージョンの場合は、DataWeave の目次のバージョンセレクターを使用できます。
次の例では、flatten
を使用して、変数 arrayOne
で定義される入力配列のキー-値ペアをフラット化しています。配列内のすべてのキー/値ペアは個別の DataWeave オブジェクトになります。
この例では、次の関数を使用します。
flatten
は、サブ配列の要素を親配列に移動して、サブ配列を除去し、すべてのキー/値ペアを親配列内のオブジェクトのリストに変換します。
上の DataWeave の例と同じように、次の例は、2 つの配列の要素を組み合わせてフラット化します。この 2 番目の例では、セレクター fruit
を使用してキー fruit
の値のみを選択し、配列内の他の値は除外します。
この例では、以下を使用します。
++
は、2 つの配列 (arrayOne
と arrayTwo
) のオブジェクトを 1 つの配列に組み合わせます。
flatten
は、組み合わせた配列を、キー/値ペアのリストとしてフラット化します。
%dw 2.0
var arrayOne = [
[
"fruit" : "orange",
"fruit" : "apple"
],
[
"fruit" : "grape",
"notfruit" : "something else"
]
]
var arrayTwo = [
[
{ "fruit" : "kiwi" }
],
"fruit" : "strawberry",
"fruit" : "plum",
{ "fruit" : "banana" },
"notfruit" : "something else"
]
output application/json
---
flatten(arrayOne ++ arrayTwo)
上の例と次の例の唯一の違いは、.fruit
セレクターを本文の式に追加して、fruit
値をすべて選択し、notfruit
キーの値を出力配列から除外しているという点です。
%dw 2.0
var arrayOne = [
[
"fruit" : "orange",
"fruit" : "apple"
],
[
"fruit" : "grape",
"notfruit" : "something else"
]
]
var arrayTwo = [
[
{ "fruit" : "kiwi" }
],
"fruit" : "strawberry",
"fruit" : "plum",
{ "fruit" : "banana" },
"notfruit" : "something else"
]
output application/json
---
flatten(arrayOne ++ arrayTwo).fruit
次の例では、本文の式で .fruit
ではなく ..*fruit
をセレクターとして使用し、ネストされているすべての fruit
値を、フラット化した配列に返しています。また、2 つではなく 3 つの配列を組み合わせてフラット化しています。
%dw 2.0
var arrayOne = [
[
"fruit" : "orange",
"fruit" : "apple"
],
[
"fruit" : "grape",
"notfruit" : "something else"
]
]
var arrayTwo = [
[
{ "fruit" : "kiwi" }
],
"fruit" : "strawberry",
"fruit" : "plum",
{ "fruit" : "banana" },
"notfruit" : "something else"
]
var arrayThree = [
{ parentOne :
[
{ child :
[
{ grandchild :
{
"fruit" : "watermelon",
"notfruit" : "something else"
}
},
{
fruit : "cantaloupe",
"notfruit" : "something else"
}
]
},
{
fruit : "honeydew",
"notfruit" : "something else"
}
]
},
{ parentTwo:
[
fruit : "cherry",
"notfruit" : "something else"
]
}
]
output application/json
---
flatten(arrayOne ++ arrayTwo ++ arrayThree)..*fruit
次の例では、flatten
関数をさまざまなデータ型 (数値、DataWeave オブジェクト、サブ配列、文字列、null
値、キー/値ペア) に対して使用しています。サブ配列の要素は親配列の要素になり、サブ配列は除去されます。
この例では、次の関数を使用します。
flatten
は、サブ配列をフラット化して親配列に含めます。