Skip to main content Skip to complementary content

Creating key/value pairs

In Talend Data Mapper, you can create structure elements with a Map group type to work with key/value pairs in your mappings.

About this task

In this example, you have an input JSON file with an items array that contains an object for each item. The goal of this transformation is to get an output JSON file with an items object that contains an object for each item with the item ID as the key. You can use the following JSON sample as input:
{
    "items": [
        {
            "id": "ABC123",
            "name": "MBIKE",
            "description": "27.5 inch electric mountain bike"
        },
        {
            "id": "DEF456",
            "name": "EBIKE",
            "description": "Electric hybrid bike"
        },
        {
            "id": "GHI789",
            "name": "WBIKE",
            "description": "Women's road bike"
        }
    ]
}

Creating an output structure with key/value pairs

Create a structure containing an element with a Map group type.

Procedure

  1. In the Data Mapper tab, expand the Hierarchical Mapper node and right-click Structures.
  2. Click New > Structure.
  3. In the wizard that opens, select Create a new structure where you manually enter elements and click Next.
  4. Select a folder and enter a name for your structure, then click Next.
  5. Select a representation, JSON in this example, and click Finish.
    An empty structure is created, you can create its elements.
  6. Right-click the section on the left of your workspace and click New Element to create a root element, and keep the default properties.
  7. Right-click the root element to create a new child element named items.
  8. In the items element's parameters, change the value of the Group Type parameter to Map.
  9. Create two child elements in the value element, named name and description.
  10. Save your structure.

Results

Your structure looks like this:
A Data Mapper structure containing a root element, an items element with the group type map in the root element, an entry loop in the items element, a key element and a value element in the entry loop, and a name element and a description element in the value element.

Mapping key/value pairs

Map elements from your input structure to create key/value pairs in your output.

Before you begin

You have an input structure. You can create one using the JSON sample provided in Creating key/value pairs.

Procedure

  1. Create a new standard map and add your input and output structures.
  2. Drag and drop the input id element on the output key element to use the item IDs as keys.
  3. Drag and drop the input name and description elements on the corresponding output elements.

Results

Your map is configured, you can use the Test Run feature to check the result. In the example, the output JSON looks like this:
{
                    "items": {
                    "ABC123": {
                    "name": "MBIKE",
                    "description": "27.5 inch electric mountain bike"
                    },
                    "DEF456": {
                    "name": "EBIKE",
                    "description": "Electric hybrid bike"
                    },
                    "GHI789": {
                    "name": "WBIKE",
                    "description": "Women's road bike"
                    }
                    }
                    }

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 – please let us know!