Tutorial: Mapping JSON Content
This tutorial shows you how to map message content from one format or schema to another format or schema.
Before You Begin
This task can be completed by users who are assigned one of the following roles:- Catalog Owner
- Catalog Administrator
Note: The Sandbox catalog must be configured to use either a DataPower® Gateway (v5
compatible) or a DataPower API
Gateway or both. See Creating and configuring Catalogs.
About this tutorial
In this tutorial you are going to complete the following lessons:Create a New API
Take the following steps to create a new API.
Map JSON Content
- Click Assemble.
- Delete the existing policy on the canvas by hovering your cursor over the policy and then clicking the Delete icon ; if you are using the DataPower API Gateway, the policy is an invoke policy whereas if you are using the DataPower Gateway (v5 compatible), the policy is a proxy policy.
- From the action palette, drag the Map action onto the dashed box that is displayed on the canvas. The configuration pane opens automatically.
- Click the Edit inputs icon.
- Click + input.
- Configure the input according to the following table:
Table 1. JSON input Property Value Context variable request.body Name input Content type application/json Definition Inline schema - A dialog pane opens. Click Generate from sample JSON.
- Paste the following sample JSON into the box.
{"order": {"customer": {"name": { "firstname":"John", "middlename":"Q", "lastname":"Smith" }, "address": { "line1":"550 King St", "line2":"Dept 5", "city":"Littleton", "state":"MA", "country":"USA", "code":"01460" } } }, "items": [{"item":"shoes", "color":"black", "qty":2, "price":23.50}, {"item":"socks", "color":"argyle", "qty":2, "price":3.95}, {"item":"pants", "color":"grey", "qty":1, "price":48.00}] }
- Click generate.
- The OpenAPI code to describe the schema appears. Click done. The dialog box closes.
- Click Done to complete the Input map configuration.
- Click the Edit outputs icon in the Output column of the property sheet.
- Click + output.
- Configure the input according to the following table:
Table 2. JSON output Property Value Context variable message.body Name output Content type application/json Definition Inline schema - A dialog pane opens. Click Generate from sample JSON.
- Paste the following example JSON into the box.
{"order": { "date":"12-12-12", "customer": "John Smith", "address": { "street":"king", "citystatezip":"lit MA 01469", "country":"USA" }, "items":[{"type":"shoes", "color":"black", "qty":2, "price":23.50}] } }
- Click generate.
- The OpenAPI code to describe the schema appears. Click done. The dialog box closes.
- Click Done.
- Click Save.
- Click the handle alongside the date field in the Output column. A dialog box opens.
- Enter the following JavaScript code in the box;
new Date().toGMTString()
, then click ok. - Click the handle alongside the firstname field in Input and click the handle alongside the customer field in Output.
- Click the handle alongside the middlename field in Input and click the handle alongside the customer field in Output.
- Click the handle alongside the lastname field in Input and click the handle alongside the customer field in Output.
- Click the handle alongside the customer field in Output. A dialog box opens.
- Enter the following code in the box. Click ok.
var name = $(input.order.customer.name.firstname) + ' '; if($(input.order.customer.name.middlename)) { name += $(input.order.customer.name.middlename) + ' ';} name += $(input.order.customer.name.lastname); name
- Click the handle alongside the address line1 field in Input and click the handle alongside the street field in Output.
- Click the handle alongside the address line2 field in Input and click the handle alongside the street field in Output.
- Click the handle alongside street in Output. This opens a dialog box.
- Enter the following code in the box. Click ok.
var street = $(input.order.customer.address.line1) + ' '; if($(input.order.customer.address.line2)) { street += $(input.order.customer.address.line2);} street
- Click the handle alongside the city field in Input and click the handle alongside the citystatezip field in Output.
- Click the handle alongside the state field in Input and click the handle alongside the citystatezip field in Output.
- Click the handle alongside the code field in Input and click the handle alongside the citystatezip field in Output.
- Click the handle alongside the citystatezip field in Output. A dialog box opens.
- Enter the following code in the box. Note that the variable references are positional; $(1)
refers to the first mapped value (input.order.customer.city), and so on. Click
ok.
$(1) + " " + $(2) + " " + $(3)
- Click the handle alongside the items field in Input and click the handle alongside the items field in Output. An additional wire is drawn automatically.
- Click add property after the closing array bracket - ] - of the Output items array.
- Enter total in the field that is labeled property. Set the type to float. Click the + icon.
- Click the handle alongside the items field in Input and click the handle alongside the total field in Output. Note that the system automatically adds a connector to the map.
- Click the handle alongside total in Output. This opens a dialog box.
- Enter the following code in the box. This code sums the items for each order. Click
ok.
$(0) + ($(input.items.price) * $(input.items.qty))
- Click the Save icon to save these changes.
- Click Source.
- Drag the highlighter to the end of the Source.
- Locate the code where total is calculated.
- Insert a new line with the code foreach: input.items.
- Click the Save icon to save these changes.
Testing your API definition
Note: Due to Cross-Origin Resource Sharing (CORS)
restrictions, the assembly test tool cannot be used with the Chrome or Safari browsers on the macOS
Catalina platform.
To test your API definition by using the API Manager test tool, complete the following steps:
Manage your API definition
Now that your new API works correctly, you can manage this API. To see your immediate options, take the following steps.
What you did in this tutorial
- Created a new API
- Mapped content from one JSON schema to another schema.