Skip to main content Skip to complementary content
Close announcements banner

Using a choice element in the input

Availability-noteBeta
Map an input file containing an element that can have different sets of children.

Before you begin

  • You have created an input and an output structure. You can use the JSON samples below to create your structures.

About this task

In this example you have an input JSON file containing information about bicycles and scooters for sale. The details element in this file can have different children depending on the type of item. When the item is a bicycle, the details element contains three elements: bicycle, seat, and derailleur. When the item is a scooter, details contains the elements scooters and maxWeight.

In the output, you want to replace the details element with either a bicycle or scooter element.

The input data looks like this:
[
    {
        "sku": "AB4589",
        "price": 899.99,
        "details": {
            "bicycle": "MBIKE",
            "seat": "ergon",
            "derailleur": "shimano"
        }
    },
    {
        "sku": "DQ5678",
        "price": 899.99,
        "details": {
            "bicycle": "WBIKE",
            "seat": "selle italia",
            "derailleur": "sram"
        }
    },
    {
        "sku": "MF5612",
        "price": 399.99,
        "details": {
            "scooter": "ESCOOT",
            "maxWeight": 70
        }
    },
    {
        "sku": "MF5612",
        "price": 399.99,
        "details": {
            "scooter": "KSCOOT",
            "maxWeight": 50
        }
    }
]
The output structure looks like this:
[
    {
        "sku": "",
        "price": 899.99,
        "bicycle": {
            "make": "",
            "seat": "",
            "derailleur": ""
        }
    },
    {
        "sku": "",
        "price": 399.99,
        "scooter": {
            "make": "",
            "maxWeight": 70
        }
    }
]

Procedure

  1. Open the input structure you created and expand the details element.
    You can see that details is a Choice element. It contains two Sequence elements, which are the two possible sets of children for details.
  2. Click Read Only and select Editable in the drop-down list.
  3. Click sequence and replace sequence with bicycle in the Name field, then rename sequence_1 to scooter and save the structure.
  4. Create a new DSQL Map and add the input and output structures.

    In the input, the details element is labeled as choice, and the two alternatives are assigned an index. This index can be used to refer to a specific alternative in an expression.

  5. Map the following elements using drag-and-drop:
    • sku to sku
    • price to price
    • bicycle {@0} to bicycle
    • bicycle to bicycle.make
    • scooter to scooter.make
    • maxWeight to maxWeight

Results

The elements are mapped, and if you click the output bicycle and scooter elements, you can see that conditional expressions have been automatically generated.
For example, IF (isPresent(details.@0)) indicates that an output bicycle element will be produced only if the input details element matches the first alternative. In this example, the Test Run returns the following result:
[
   {
      "sku":"AB4589",
      "price":899.99,
      "bicycle":{
         "make":"MBIKE",
         "seat":"ergon",
         "derailleur":"shimano"
      }
   },
   {
      "sku":"DQ5678",
      "price":899.99,
      "bicycle":{
         "make":"WBIKE",
         "seat":"selle italia",
         "derailleur":"sram"
      }
   },
   {
      "sku":"MF5612",
      "price":399.99,
      "scooter":{
         "make":"ESCOOT",
         "maxWeight":70
      }
   },
   {
      "sku":"MF5612",
      "price":399.99,
      "scooter":{
         "make":"KSCOOT",
         "maxWeight":50
      }
   }
]

If you removed the conditions from the bicycle and scooter elements, each output would contain both elements, and one of them would be empty.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!