Skip to main content Skip to complementary content
Close announcements banner

Using key/value pairs

Availability-noteBeta
Update a structure to be able to map key/value pairs.

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 a JSON file with an items object that contains information about bicycles and scooters. Each item is an object with the item name as the key, and the value is an object containing the description and the price of the item. The goal is to create an output with an array of items.

The input data looks like this:
{
    "items": {
        "MBIKE": {
            "description": "27.5 inch electric mountain bike",
            "price": "899.99"
        },
        "EBIKE": {
            "description": "Electric hybrid bike",
            "price": "999.99"
        },
        "WBIKE": {
            "description": "Women's road bike",
            "price": "299.99"
        },
        "ESCOOT": {
            "description": "Camou electric scooter",
            "price": "749.0"
        },
        "EVSCOOT": {
            "description": "Folding electric velocity+ scooter",
            "price": "599.99"
        },
        "KSCOOT": {
            "description": "24 volt kid scooter",
            "price": "299.99"
        }
    }
}
The output structure looks like this:
{
    "items": [
        {
            "make": "",
            "description": "",
            "price": ""
        }
    ]
}

Procedure

  1. Open the input structure you created and expand the items element.
    The items element contains one child for each type of item present in the JSON file. This means the structure would not be usable if more items were added, so you need to update the structure to make it generic.
  2. Click Read Only and select Editable in the drop-down list.
  3. Click items and select Map in the Group Type drop-down list.
    If you have installed the R2023-11 Talend Studio monthly update or a later one, a dialog box appears to indicate that changing the element type to Map will replace the element's children. If you click Yes, an entry loop containing a key element and a value element will be created automatically.

    If you are using an older monthly update of Talend Studio, you should manually delete the elements in items and create the entry loop with the key and value elements.

  4. Right-click value and create two new elements named description and price, then save the structure.
    The final structure looks like this:

    You can close and reopen the structure to check the highlighting in the Document tab.

  5. Create a new DSQL Map and add the input and output structures.
  6. Map the following elements using drag-and-drop:
    • key to make
    • description to description
    • price to price
  7. Click items and add the following line under the existing expression to filter the output data:
    WHERE value.price <= items['ESCOOT'].price

    This expression indicates that the map should only return items with a price lower than or equal to the price of the ESCOOT input element. You can see that the value is referenced using a specific key. For more information, see the Talend Data Shaping Language Reference Guide.

Results

Your map is configured, you can use the Test Run feature to check the result. In this example, the following result is returned:
{
   "items":[
      {
         "make":"ESCOOT",
         "description":"Camou electric scooter",
         "price":"749.0"
      },
      {
         "make":"WBIKE",
         "description":"Women's road bike",
         "price":"299.99"
      },
      {
         "make":"KSCOOT",
         "description":"24 volt kid scooter",
         "price":"299.99"
      },
      {
         "make":"EVSCOOT",
         "description":"Folding electric velocity+ scooter",
         "price":"599.99"
      }
   ]
}
You can also do the opposite and use a map to create an output with key/value pairs, for example:

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!