ニュース記事のサンプル (API Gateway のモデルとマッピングテンプレート) - Amazon API Gateway

ニュース記事のサンプル (API Gateway のモデルとマッピングテンプレート)

以下のセクションに、API Gateway でのニュース記事 API のサンプルに使用できるモデルとマッピングテンプレートの例を示します。API Gateway のモデルとマッピングテンプレートの詳細については、「PetStore マッピングテンプレート」を参照してください。

元のデータ (ニュース記事サンプル)

以下に、ニュース記事サンプルの元の JSON データを示します。

{ "count": 1, "items": [ { "last_updated_date": "2015-04-24", "expire_date": "2016-04-25", "author_first_name": "John", "description": "Sample Description", "creation_date": "2015-04-20", "title": "Sample Title", "allow_comment": "1", "author": { "last_name": "Doe", "email": "johndoe@example.com", "first_name": "John" }, "body": "Sample Body", "publish_date": "2015-04-25", "version": "1", "author_last_name": "Doe", "parent_id": 2345678901, "article_url": "http://www.example.com/articles/3456789012" } ], "version": 1 }

入力モデル (ニュース記事サンプル)

以下に、ニュース記事サンプルの元の JSON データに対応する入力モデルを示します。

{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "NewsArticleInputModel", "type": "object", "properties": { "count": { "type": "integer" }, "items": { "type": "array", "items": { "type": "object", "properties": { "last_updated_date": { "type": "string" }, "expire_date": { "type": "string" }, "author_first_name": { "type": "string" }, "description": { "type": "string" }, "creation_date": { "type": "string" }, "title": { "type": "string" }, "allow_comment": { "type": "string" }, "author": { "type": "object", "properties": { "last_name": { "type": "string" }, "email": { "type": "string" }, "first_name": { "type": "string" } } }, "body": { "type": "string" }, "publish_date": { "type": "string" }, "version": { "type": "string" }, "author_last_name": { "type": "string" }, "parent_id": { "type": "integer" }, "article_url": { "type": "string" } } } }, "version": { "type": "integer" } } }

入力マッピングテンプレート (ニュース記事サンプル)

以下に、ニュース記事サンプルの元の JSON データに対応する入力マッピングテンプレートを示します。

#set($inputRoot = $input.path('$')) { "count": $inputRoot.count, "items": [ #foreach($elem in $inputRoot.items) { "last_updated_date": "$elem.last_updated_date", "expire_date": "$elem.expire_date", "author_first_name": "$elem.author_first_name", "description": "$elem.description", "creation_date": "$elem.creation_date", "title": "$elem.title", "allow_comment": "$elem.allow_comment", "author": { "last_name": "$elem.author.last_name", "email": "$elem.author.email", "first_name": "$elem.author.first_name" }, "body": "$elem.body", "publish_date": "$elem.publish_date", "version": "$elem.version", "author_last_name": "$elem.author_last_name", "parent_id": $elem.parent_id, "article_url": "$elem.article_url" }#if($foreach.hasNext),#end #end ], "version": $inputRoot.version }

変換されたデータ (ニュース記事サンプル)

以下に、ニュース記事サンプルの元の JSON データが出力用に変換されるプロセスの一例を示します。

{ "count": 1, "items": [ { "creation_date": "2015-04-20", "title": "Sample Title", "author": "John Doe", "body": "Sample Body", "publish_date": "2015-04-25", "article_url": "http://www.example.com/articles/3456789012" } ], "version": 1 }

出力モデル (ニュース記事サンプル)

以下に、変換された JSON データ形式に対応する出力モデルを示します。

{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "NewsArticleOutputModel", "type": "object", "properties": { "count": { "type": "integer" }, "items": { "type": "array", "items": { "type": "object", "properties": { "creation_date": { "type": "string" }, "title": { "type": "string" }, "author": { "type": "string" }, "body": { "type": "string" }, "publish_date": { "type": "string" }, "article_url": { "type": "string" } } } }, "version": { "type": "integer" } } }

出力マッピングテンプレート (ニュース記事サンプル)

次に、変換された JSON データ形式に対応する出力マッピングテンプレートを示します。この場合のテンプレートは、変換されていない元の JSON データ形式に基づいて変化します。

#set($inputRoot = $input.path('$')) { "count": $inputRoot.count, "items": [ #foreach($elem in $inputRoot.items) { "creation_date": "$elem.creation_date", "title": "$elem.title", "author": "$elem.author.first_name $elem.author.last_name", "body": "$elem.body", "publish_date": "$elem.publish_date", "article_url": "$elem.article_url" }#if($foreach.hasNext),#end #end ], "version": $inputRoot.version }