オブジェクト要素の配列としてのマップ

この DataWeave の例では、DataWeave map 関数を使用して、キー book に一致するオブジェクト要素を反復処理します。入力には、無視されるキー magazine も含まれます。

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

  • キー「book」に一致する要素が含まれる配列を返す複数値セレクタ *book

  • 複数値セレクタによって返される配列の各オブジェクトを調べる map

DataWeave
%dw 2.0
output application/json
---
items: payload.inventory.*book map (item, index) -> {
      "type": "book",
      "id": index,
      "category": item.category,
      "title": item.text,
      "author": item.author,
      "year": item.year,
      "price": item.price
}
入力
{
  "inventory": {
      "book" : {
        "-category": "cooking",
        "title": "Everyday Italian",
        "author": "Giada De Laurentiis",
        "year": "2005",
        "price": "30.00"
      },
      "book" :{
        "-category": "children",
        "title": "Harry Potter",
        "author": "J K. Rowling",
        "year": "2005",
        "price": "29.99"
      },
      "book" :{
        "-category": "web",
        "-cover": "paperback",
        "title": "Learning XML",
        "author": "Erik T. Ray",
        "year": "2003",
        "price": "39.95"
      },
      "magazine" :{
        "-category": "web",
        "title": "Wired Magazine",
        "edition": "03-2017",
        "price": "15.95"
      },
        "magazine" :{
        "-category": "business",
        "title": "Time Magazine",
        "edition": "04-2017",
        "price": "17.95"
      }
   }
}
出力
{
  "items": [
    {
      "type": "book",
      "id": 0,
      "category": null,
      "title": null,
      "author": "Giada De Laurentiis",
      "year": "2005",
      "price": "30.00"
    },
    {
      "type": "book",
      "id": 1,
      "category": null,
      "title": null,
      "author": "J K. Rowling",
      "year": "2005",
      "price": "29.99"
    },
    {
      "type": "book",
      "id": 2,
      "category": null,
      "title": null,
      "author": "Erik T. Ray",
      "year": "2003",
      "price": "39.95"
    }
  ]
}

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub