Articles in this section

Mapper 2.0: Iterate over an array

When your source JSON data contains an array of items that you want to independently map, use the bracketed wildcard syntax [*] on the parent object, then use dot notation to select each object in the array. For example, consider the following source data:

{
  "orderId": 123,
  "total": 99.99,
  "state": "ca",
  "items": [
    {
      "description": "Hat",
      "qty": 3
    },
    {
      "description": "Shoes",
      "qty": 1
    }
  ]
}

In the above JSON, the items array contains multiple objects, each of which has a description field and a qty field. Also notice that the orderId field is not included in the items array.

For this example, the destination application requires the following format:

{
  "items": [
    {
      "description": "Hat",
      "quantity": 3,
      "orderID": 123
    },
    {
      "description": "Shoes",
      "quantity": 1,
      "orderID": 123
    }
  ]
}

Use the following steps to generate the above destination JSON structure from the source data using Mapper 2.0.

mapper2IteratingOverArray.png

Destination (left column)

Source (right column)

  • Create the items object array in the left column (destination).

    1. Name the destination object array items .

    2. Set the data type to [object].

  • In the right column (source), use JSONPath notation to reference the items object array from the source data: $.items[*].

  • Add the description string field to the left column (destination).

  • Select the description field from the right column (source): @.items.description.

  • Add the quantity number field to the destination.

  • Select the qty field from the source data: @.items.qty.

  • Add the orderID number field to the destination object.

  • Select the orderId from the source data: $.orderId.

Tip

Tips: In this example, notice the following:

  • Specify the wildcard [*] to select all fields returned within the parent object array ( items). Never use wildcard syntax to select the child fields contained within an array of objects. Instead, use dot notation to select each child field.

  • You can add fields from any part of the source data as nested fields in a destination JSON object array. The above example adds orderId to the objects included in the destination items array.

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.