DataWeave を使用したデータのマップ

この DataWeave の例では、DataWeave map 関数を使用して、本の配列を反復処理し、それぞれで一連のタスクを実行します。

次の DataWeave 関数を使用します。

  • books 配列の各オブジェクトを調べる map

  • 価格データを Number (数値) 型に強制変換する as。これにより、変換で各要素の正しい型が生成されます。

DataWeave
%dw 2.0
output application/json
---
items: payload.books map ((item, index) -> {
      category: "book",
      price: item.price as Number,
      id: index,
      properties: {
        title: item.title,
        author: item.author,
        year: item.year as Number
      }
   }
)
入力 JSON
{
    "books": [
      {
        "-category": "cooking",
        "title": {
          "-lang": "en",
          "#text": "Everyday Italian"
        },
        "author": "Giada De Laurentiis",
        "year": "2005",
        "price": "30.00"
      },
      {
        "-category": "children",
        "title": {
          "-lang": "en",
          "#text": "Harry Potter"
        },
        "author": "J K. Rowling",
        "year": "2005",
        "price": "29.99"
      },
      {
        "-category": "web",
        "title": {
          "-lang": "en",
          "#text": "XQuery Kick Start"
        },
        "author": [
          "James McGovern",
          "Per Bothner",
          "Kurt Cagle",
          "James Linn",
          "Vaidyanathan Nagarajan"
        ],
        "year": "2003",
        "price": "49.99"
      },
      {
        "-category": "web",
        "-cover": "paperback",
        "title": {
          "-lang": "en",
          "#text": "Learning XML"
        },
        "author": "Erik T. Ray",
        "year": "2003",
        "price": "39.95"
      }
    ]
}
出力 JSON
{
  "items": [
    {
      "category": "book",
      "price": 30.00,
      "id": 0,
      "properties": {
        "title": {
          "-lang": "en",
          "#text": "Everyday Italian"
        },
        "author": "Giada De Laurentiis",
        "year": 2005
      }
    },
    {
      "category": "book",
      "price": 29.99,
      "id": 1,
      "properties": {
        "title": {
          "-lang": "en",
          "#text": "Harry Potter"
        },
        "author": "J K. Rowling",
        "year": 2005
      }
    },
    {
      "category": "book",
      "price": 49.99,
      "id": 2,
      "properties": {
        "title": {
          "-lang": "en",
          "#text": "XQuery Kick Start"
        },
        "author": [
          "James McGovern",
          "Per Bothner",
          "Kurt Cagle",
          "James Linn",
          "Vaidyanathan Nagarajan"
        ],
        "year": 2003
      }
    },
    {
      "category": "book",
      "price": 39.95,
      "id": 3,
      "properties": {
        "title": {
          "-lang": "en",
          "#text": "Learning XML"
        },
        "author": "Erik T. Ray",
        "year": 2003
      }
    }
  ]
}
本に複数の著者がいる場合、item.author は 1 つの名前ではなく著者の配列全体に評価されます。

デフォルト値の使用

次の例では、上記と同じ変換を実行しますが、プロパティ「item」および「index」を明示的に定義しません。代わりに、それぞれのデフォルト名 ($ および $$) でコールします。

DataWeave
%dw 2.0
output application/json
---
items: (payload.books map {
      category: "book",
      price: $.price as Number,
      id: $$,
      properties: {
        title: $.title,
        author: $.author,
        year: $.year as Number
      }
})

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub