Menu
Amazon API Gateway
Developer Guide

Sales Invoice Example (API Gateway Models and Mapping Templates)

The following sections provide examples of models and mapping templates that could be used for a sample sales invoice API in API Gateway. For more information about models and mapping templates in API Gateway, see Create Models and Mapping Templates for Request and Response Payloads.

Original Data (Sales Invoice Example)

The following is the original JSON data for the sales invoice example:

Copy
{ "DueDate": "2013-02-15", "Balance": 1990.19, "DocNumber": "SAMP001", "Status": "Payable", "Line": [ { "Description": "Sample Expense", "Amount": 500, "DetailType": "ExpenseDetail", "ExpenseDetail": { "Customer": { "value": "ABC123", "name": "Sample Customer" }, "Ref": { "value": "DEF234", "name": "Sample Construction" }, "Account": { "value": "EFG345", "name": "Fuel" }, "LineStatus": "Billable" } } ], "Vendor": { "value": "GHI456", "name": "Sample Bank" }, "APRef": { "value": "HIJ567", "name": "Accounts Payable" }, "TotalAmt": 1990.19 }

Input Model (Sales Invoice Example)

The following is the input model that corresponds to the original JSON data for the sales invoice example:

Copy
{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "InvoiceInputModel", "type": "object", "properties": { "DueDate": { "type": "string" }, "Balance": { "type": "number" }, "DocNumber": { "type": "string" }, "Status": { "type": "string" }, "Line": { "type": "array", "items": { "type": "object", "properties": { "Description": { "type": "string" }, "Amount": { "type": "integer" }, "DetailType": { "type": "string" }, "ExpenseDetail": { "type": "object", "properties": { "Customer": { "type": "object", "properties": { "value": { "type": "string" }, "name": { "type": "string" } } }, "Ref": { "type": "object", "properties": { "value": { "type": "string" }, "name": { "type": "string" } } }, "Account": { "type": "object", "properties": { "value": { "type": "string" }, "name": { "type": "string" } } }, "LineStatus": { "type": "string" } } } } } }, "Vendor": { "type": "object", "properties": { "value": { "type": "string" }, "name": { "type": "string" } } }, "APRef": { "type": "object", "properties": { "value": { "type": "string" }, "name": { "type": "string" } } }, "TotalAmt": { "type": "number" } } }

Input Mapping Template (Sales Invoice Example)

The following is the input mapping template that corresponds to the original JSON data for the sales invoice example:

Copy
#set($inputRoot = $input.path('$')) { "DueDate": "$inputRoot.DueDate", "Balance": $inputRoot.Balance, "DocNumber": "$inputRoot.DocNumber", "Status": "$inputRoot.Status", "Line": [ #foreach($elem in $inputRoot.Line) { "Description": "$elem.Description", "Amount": $elem.Amount, "DetailType": "$elem.DetailType", "ExpenseDetail": { "Customer": { "value": "$elem.ExpenseDetail.Customer.value", "name": "$elem.ExpenseDetail.Customer.name" }, "Ref": { "value": "$elem.ExpenseDetail.Ref.value", "name": "$elem.ExpenseDetail.Ref.name" }, "Account": { "value": "$elem.ExpenseDetail.Account.value", "name": "$elem.ExpenseDetail.Account.name" }, "LineStatus": "$elem.ExpenseDetail.LineStatus" } }#if($foreach.hasNext),#end #end ], "Vendor": { "value": "$inputRoot.Vendor.value", "name": "$inputRoot.Vendor.name" }, "APRef": { "value": "$inputRoot.APRef.value", "name": "$inputRoot.APRef.name" }, "TotalAmt": $inputRoot.TotalAmt }

Transformed Data (Sales Invoice Example)

The following is one example of how the original sales invoice example JSON data could be transformed for output:

Copy
{ "DueDate": "2013-02-15", "Balance": 1990.19, "DocNumber": "SAMP001", "Status": "Payable", "Line": [ { "Description": "Sample Expense", "Amount": 500, "DetailType": "ExpenseDetail", "Customer": "ABC123 (Sample Customer)", "Ref": "DEF234 (Sample Construction)", "Account": "EFG345 (Fuel)", "LineStatus": "Billable" } ], "TotalAmt": 1990.19 }

Output Model (Sales Invoice Example)

The following is the output model that corresponds to the transformed JSON data format:

Copy
{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "InvoiceOutputModel", "type": "object", "properties": { "DueDate": { "type": "string" }, "Balance": { "type": "number" }, "DocNumber": { "type": "string" }, "Status": { "type": "string" }, "Line": { "type": "array", "items": { "type": "object", "properties": { "Description": { "type": "string" }, "Amount": { "type": "integer" }, "DetailType": { "type": "string" }, "Customer": { "type": "string" }, "Ref": { "type": "string" }, "Account": { "type": "string" }, "LineStatus": { "type": "string" } } } }, "TotalAmt": { "type": "number" } } }

Output Mapping Template (Sales Invoice Example)

The following is the output mapping template that corresponds to the transformed JSON data format. The template variables here are based on the original, not transformed, JSON data format:

Copy
#set($inputRoot = $input.path('$')) { "DueDate": "$inputRoot.DueDate", "Balance": $inputRoot.Balance, "DocNumber": "$inputRoot.DocNumber", "Status": "$inputRoot.Status", "Line": [ #foreach($elem in $inputRoot.Line) { "Description": "$elem.Description", "Amount": $elem.Amount, "DetailType": "$elem.DetailType", "Customer": "$elem.ExpenseDetail.Customer.value ($elem.ExpenseDetail.Customer.name)", "Ref": "$elem.ExpenseDetail.Ref.value ($elem.ExpenseDetail.Ref.name)", "Account": "$elem.ExpenseDetail.Account.value ($elem.ExpenseDetail.Account.name)", "LineStatus": "$elem.ExpenseDetail.LineStatus" }#if($foreach.hasNext),#end #end ], "TotalAmt": $inputRoot.TotalAmt }