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
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.

  1. Log in to API Manager.
  2. In the Home page, click the Develop APIs and Products tile.

    Alternatively, click the Develop icon on the left panel.

    APIM screen

  3. In the Develop page, click Add > API.

    API add menu

  4. In the Select API type page,
    1. Ensure that OpenAPI 2.0 is selected.
    2. Select the New OpenAPI radio button.
    3. Click Next.

      New API

  5. In the Create new OpenAPI page, enter the following information about the API.
    Note: The OpenAPI Version field is per-defined. You can't update this field.
    1. In the Title field, enter Mapper.

      The Name field is filled with the value that you enter in the Title field.

    2. In the Version field, enter 1.0.0.
    3. In the Base path field, enter /map .
    4. Click Next.

  6. Under the Secure section,
    1. The Secure using Client ID and the CORS check boxes are per-selected. Don't deselect the check boxes.
    2. Click Next.

      Secure API

      You see the progress as the new API is created. When the API is created, you see a Summary.

  7. Under the Summary section, Click Edit API.

    New API summary

    The Design tab for the draft of your API definition is displayed.

  8. In the left panel, click Consumes.
  9. In the right panel, in the Consumes section,
    1. Click Add +.
    2. In the Add schema dialog box, from the Mime Type (optional) drop down list, select application/json and then click Add.

  10. In the left panel, click Produces.
  11. In the right panel, in the Produces section,
    1. Click Add +.
    2. In the Add schema dialog box, from the Mime Type (optional) drop down list, select application/json and then click Add.

  12. In the left panel, click Paths.
  13. In the right panel, in the Paths section,
    1. Click Add +.
    2. In the Path name field, enter /jsonmap and then click Add.

  14. In the left panel, under the newly created path (/jsonmap), beside Operations, click the + icon.

    The Add schema dialog box is displayed.

  15. In the Add schema dialog box, perform the following tasks.
    1. In the Operation (optional) field, enter POST.
    2. Click Add.

  16. In the left panel, beside Parameters, click the + icon.

    The Add schema dialog box is displayed.

  17. In the Add schema dialog box, perform the following tasks.
    1. In the Name field, enter body.
    2. Select the Required checkbox.
    3. From the Located In drop down list, select body.
    4. Click Add.

  18. Scroll down to the Schema section and then click View.
  19. Under the Schema section, perform the following tasks.
    1. In the Title (optional) filed, enter body.
    2. From the Type drop down list, select string.
    3. On the top right corner, click Save.

  20. In the left panel, expand Paths > Operations > Responses.
  21. Under Responses, click 200.
  22. In the right panel, under the 200 section, perform the following tasks.
    1. In the Name (Key) filed, enter 200.
    2. In the Description field, enter 200 OK.
    3. On the top right corner, click Save.

Map JSON Content

  1. Click the Gateway tab.
  2. 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.

  3. To view the palette, in the right panel, click the plus icon .

  4. Under the Transforms column, select map.

    The map policy node is displayed on the canvas.

  5. Click the map policy node to open the configuration dialog box.
  6. Adjacent to Input, click the Edit inputs icon.

  7. Click Add input.
  8. Complete the map input configuration by entering the following values
    1. In the Context variable field, enter request.body.
    2. In the Name field, enter input.
    3. From the Content type drop down list, select application/json.
    4. From the Definition drop down list, select Inline schema.

      The Provide a schema pop up window is displayed.

    5. In the Provide a schema pop up window, perform the following tasks.
      1. Select the Generate from sample JSON tab.
      2. 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}]
        }
      3. Click Generate.

      4. Click Done.

        The pop up window is now closed.

    6. In the map policy configuration dialog box, click Done to complete the map configuration.
  9. Adjacent to the Output column, click the Edit outputs icon .

  10. Click Add output.
  11. Complete the map output configuration by entering the following values
    1. In the Context variable field, enter message.body.
    2. In the Name field, enter output.
    3. From the Content type drop down list, select application/json.
    4. From the Definition drop down list, select Inline schema.

      The Provide a schema pop up window is displayed.

    5. In the Provide a schema pop up window, perform the following tasks.
      1. Select the Generate from sample JSON tab.
      2. 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}]
        }
        }
      3. Click Generate.

    6. Click Done.

      The pop up window is now closed.

    7. In the map policy configuration dialog box, click Done to complete the map configuration.

  12. Click Save.
  13. In the map output configuration window, on the Output column, perform the following task.
    1. Select the date radio button.

      The Configure mapping dialog box is displayed.

      1. In the Configure mapping dialog box, enter the following sample JavaScript.

        new Date().toGMTString()

      2. Click Done.

    2. 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.

      1. In the Configure mapping dialog box, select the Empty string as default value check box.
      2. Click Done.
    3. 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.

      1. In the Configure mapping dialog box, select the Empty string as default value check box.
      2. Click Done.
    4. 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.

      1. In the Configure mapping dialog box, select the Empty string as default value check box.
      2. Click Done.

    5. Under the Output column, select the customer radio button.

      The Configure mapping dialog box is displayed.

      1. 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
      2. Click Done.

    6. Under the Input column, select the address > line1 radio button and then under the Output column, select the street radio button.

      The Configure mapping dialog box is displayed.

      1. In the Configure mapping dialog box, select the Empty string as default value check box.
      2. Click Done.
    7. Under the Input column, select the address > line2 radio button and then under the Output column, select the street radio button.

      The Configure mapping dialog box is displayed.

      1. In the Configure mapping dialog box, select the Empty string as default value check box.
      2. Click Done.

    8. Under the Output column, select the street radio button.

      The Configure mapping dialog box is displayed.

      1. 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
      2. Click Done.

    9. 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.

      1. In the Configure mapping dialog box, select the Empty string as default value check box.
      2. Click Done.
    10. 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.

      1. In the Configure mapping dialog box, select the Empty string as default value check box.
      2. Click Done.
    11. 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.

      1. In the Configure mapping dialog box, select the Empty string as default value check box.
      2. Click Done.

    12. Under the Output column, select the street radio button.

      The Configure mapping dialog box is displayed.

      1. 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.
      2. Click Done.

    13. Under the Input column, select the items > item radio button and then under the Output column, select the items radio button.

      The Configure mapping dialog box is displayed.

      1. In the Configure mapping dialog box, select the Empty string as default value check box.
      2. Click Done.

    14. Under the Output column, click the second add property.

      1. In the first field, enter total.
      2. From the drop down list, select float.
      3. Click the + icon.

    15. 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.

      1. In the Configure mapping dialog box, in the Value field, enter the following sample code.
        $(0) + ($(input.items.price) * $(input.items.qty))

      2. Click Done.

    16. On the map policy node output page, click Save.
  14. Click Source.
  15. Scroll down and locate the code where total is calculated.

    Calculation of total

  16. Insert a new line with the code foreach: input.items.

    Calculation of total adjusted

  17. Click Save.

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:

  1. Click the Assemble.

    Assemble page

  2. Click the Test icon The Test icon. The test tool opens.
  3. Click Activate API.

    Test setup

  4. In the Operation field, select post /jsonmap.

    Test select op

  5. In the body field, enter the following JSON text.
    {"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}]
    }

    Test select op

  6. Click Invoke. The response is displayed. Note that the values of the date, street, citystatezip, and total fields reflect the mapping formulas that you defined previously.
    Note: If you are using a self-signed certificate, you might be prompted to visit a provided URL. Click the link and then accept the certificate before returning to API Manager and clicking Invoke again.

    Test success

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.

  1. On the Navigation panel, click the Develop icon .

  2. Adjacent to the Mapper API, click the Menu icon The options icon and then select Download.

What you did in this tutorial

  • Created a new API
  • Mapped content from one JSON schema to another schema.