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
- Developer
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 the Gateway tab.
- Delete the existing policy on the canvas by hovering your cursor over the policy and then
clicking the Delete icon
.
Note: 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. - To view the palette, in the right panel, click the plus icon
.
- Under the Transforms column, select map.
The map policy node is displayed on the canvas.
- Click the map policy node to open the configuration dialog box.
- Adjacent to Input, click the Edit inputs icon.
- Click Add input.
- Complete the map input configuration by entering the following values
- In the Context variable field, enter request.body.
- In the Name field, enter input.
- From the Content type drop down list, select application/json.
- From the Definition drop down list, select Inline
schema.
The Provide a schema pop up window is displayed.
- In the Provide a schema pop up window, perform the following tasks.
- Select the Generate from sample JSON tab.
- 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.
- Click Done.
The pop up window is now closed.
- In the map policy configuration dialog box, click Done to complete the map configuration.
- Adjacent to the Output column, click the Edit
outputs icon
.
- Click Add output.
- Complete the map output configuration by entering the following values
- In the Context variable field, enter message.body.
- In the Name field, enter output.
- From the Content type drop down list, select application/json.
- From the Definition drop down list, select Inline
schema.
The Provide a schema pop up window is displayed.
- In the Provide a schema pop up window, perform the following tasks.
- Select the Generate from sample JSON tab.
- Paste the following sample 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.
- Click Done.
The pop up window is now closed.
- In the map policy configuration dialog box, click
Done to complete the map configuration.
- Click Save.
- In the map output configuration window, on the Output column, perform
the following task.
- Select the date radio button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, enter the following sample
JavaScript.
new Date().toGMTString()
- Click Done.
- In the Configure mapping dialog box, enter the following sample
JavaScript.
- Under the Input column, select the firstname radio
button and then under the Output column, select the
customer radio button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, select the Empty string as default value check box.
- Click Done.
- Under the Input column, select the middlename
radio button and then under the Output column, select the
customer radio button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, select the Empty string as default value check box.
- Click Done.
- Under the Input column, select the lastname radio
button and then under the Output column, select the
customer radio button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, select the Empty string as default value check box.
- Click Done.
- Under the Output column, select the customer radio
button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, in the Value
field, enter the following sample code.
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 Done.
- In the Configure mapping dialog box, in the Value
field, enter the following sample code.
- Under the Input column, select the
radio button
and then under the Output column, select the street
radio button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, select the Empty string as default value check box.
- Click Done.
- Under the Input column, select the
radio button
and then under the Output column, select the street
radio button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, select the Empty string as default value check box.
- Click Done.
- Under the Output column, select the street radio
button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, in the Value
field, enter the following sample code.
var street = $(input.order.customer.address.line1) + ' '; if($(input.order.customer.address.line2)) { street += $(input.order.customer.address.line2);} street
- Click Done.
- In the Configure mapping dialog box, in the Value
field, enter the following sample code.
- Under the Input column, select the city radio
button and then under the Output column, select the
citystatezip radio button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, select the Empty string as default value check box.
- Click Done.
- Under the Input column, select the state radio
button and then under the Output column, select the
citystatezip radio button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, select the Empty string as default value check box.
- Click Done.
- Under the Input column, select the code radio
button and then under the Output column, select the
citystatezip radio button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, select the Empty string as default value check box.
- Click Done.
- Under the Output column, select the street radio
button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, in the Value
field, enter the following sample code.
$(1) + " " + $(2) + " " + $(3)
Note: The variable references are positional; $(1) refers to the first mapped value (input.order.customer.city
), and so on. - Click Done.
- In the Configure mapping dialog box, in the Value
field, enter the following sample code.
- Under the Input column, select the
radio button and
then under the Output column, select the items radio
button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, select the Empty string as default value check box.
- Click Done.
- Under the Output column, click the second add
property.
- In the first field, enter total.
- From the drop down list, select float.
- Click the + icon.
- Under the Input column, select the items radio
button and then under the Output column, select the
total radio button.
The Configure mapping dialog box is displayed.
- In the Configure mapping dialog box, in the Value
field, enter the following sample code.
$(0) + ($(input.items.price) * $(input.items.qty))
- Click Done.
- In the Configure mapping dialog box, in the Value
field, enter the following sample code.
- On the map policy node output page, click Save.
- Select the date radio button.
- Click Source.
- Scroll down and locate the code where total is calculated.
- Insert a new line with the code foreach: input.items.
- Click Save.
Testing your API definition
To test your API definition by using the API Manager test tool, complete the following steps:
Manage your API definition
With your new API now working as expected, you can begin managing it. To see your immediate options, perform the following steps.
What you did in this tutorial
- Created a new API
- Mapped content from one JSON schema to another schema.