Data mapper
Hi there! In this tutorial we are going to talk about the Mapper Connector and its power in dealing with data operations.
Whenever you need to work with or prepare data, including string manipulation, arrays, formatting data, restructuring and parsing JSON schemas, dates, calculations, passing data between steps, and more, the best approach is to use the Mapper Connector.
In this tutorial we are going to show you 4 different examples:
First: build a JSON object.
Second: build an array of JSON objects.
Third: build an advanced array of JSON objects include calculation.
Forth: Filter array of JSON using function expression syntax then the same using connector.
Let’s start with the first example, by building a JSON object using the Mapper connector. The payload consists of 3 keys: first Name, country and city.
Also It’s important to consider using single quotes rather than double quotes while building your JSON object.
Now we run the workflow after setting our mapper as a starting point. We will use the Endpoint API that the workflow provides and run it from the browser.
As you can see, the workflow returns a JSON payload as expected.
The second example we will demonstrate how to add the JSON we've built in the first step into an array. For this, we will use an additional mapper connector and the "Add" array function expression.
As you can see, we picked up the “Build JSON Object” then we added it into an empty array using the Add array function expression.
Let’s test the workflow again. Now the response return an array of JSON objects.
The third example we will demonstrate a more advanced use case. We will design a logic that returns an array of JSON objects. Each of the 10 objects in this array will have an index key representing the position of this object into the array, in addition to the first name, country and city keys.
To achieve that we need:
A For Loop connector to iterate 10 times.
An incremental index variable to store the position of the iteration.
A variable to store and update the array during each iteration
A variable to store the position of the iteration.
Finally a mapper to return the Output response.
Let’s add the FOR LOOP and the VARIABLE SETTER connectors to the design space.
From the Variable Setter, we can define 2 workflow variables: var list persons and var loop.
The next step is to initiate the variables by giving them initial values. We can initiate multiple variables using the same variable setter connector.
Let’s move to the FOR LOOP part and set its index length 10. This means the loop will iterate 10 times.
Now let’s add an additional variable setter connector to calculate the defined variable during the iteration.
For the var list persons variable, we are going to add a json object during each iteration. This object consists of the index, first name, country and city key.
We will map the index to the var loop variable.
Also we will give fix values for other key, but after the first name we will add an uuid.
Regarding the var loop variable, we are going to increment it by 1 in each iteration.
Let’s connect the steps together and don’t forget to close the loop by linking the last step to the loop step.
Finally, we are going to add a mapper connector step to return the var list persons variable as response output
Now, let’s see the result by calling the workflow from the browser.
As you can see, the workflow returns an array of json response include the index in each object as expected.
The forth example we will demonstrate how to filter an array of objects using either the function expression syntax or connector.
Using the output mapper connector from the previous example, we are going to add filter key syntax. The goal is to filter the array where index value equals exactly number 4.
Let’s try to run the workflow. As we can see, it returns the expected result.
Now let’s try the same filter expression but using the FILTER connector. It’s doing the logic, but it’s more easy to use.
As you see, the workflow returns the result as expected.
Thank you for watching this tutorial. Please feel free to contact us if you have any questions.
Last updated