JSON Data Transfer and Usage Between Tasks
Scenario: An API call will be made that returns JSON data. Each element in the array within the returned result will be sent to another API with a specific format.
In this case, two database connectors should be added as a task. The first is required for reading from the database and the other for writing.
Task-1: Reading data with the API Call connector
- In the dialogue where tasks are listed, you click on the API Call option.
- To specify that the task will run only once, you select the "Once" option.
- The relevant API access information for data retrieval is entered.
- In the next task, to be able to use the data returned from the API, the "jsonResponse" value is assigned to the output key. (You can name this value as you wish.)
When this task is executed, the example returns the following JSON data; that is, the "jsonResponse" value will contain the following data:
{ "users": [ { "id": 1, "name": "user 1", "description": "Description of user 1" }, { "id": 2, "name": "user 2", "description": "Description of user 2" }, { "id": 3, "name": "user 3", "description": "Description of user 3" } ] } |
Task-2: Sending data to the API using the API Call connector
- Click on the API Call option in the dialogue where tasks are listed.
- Select the "Loop" option to indicate that the task will iterate over the array data received from the previous task.
- Specify the JSONPath indicating where the value of the entered output key ("jsonResponse") from the previous task needs to be processed as an array.
- When writing JSON Path, to indicate that processing should be done on the previous data as JSON, the "#." symbol is added to the key information ("jsonResponse"). After that, the path where the array elements are located is entered as a JSON Path. To specify that all elements of the array will be processed, the "[*]" expression is added. In this case, for this example, the JSON Path should be written as follows: {{jsonResponse#.users[*]}}
- Enter the relevant API access information where the data is to be sent.
- Enter the template message information you want to send. If you want to use the values of elements in the loop within the template message, you can include the data from the respective element in the template using the "LOOP_VARIABLE" expression. Along with "LOOP_VARIABLE," you should provide the JSON Path information pointing to the JSON data within the element designated for the loop.
In this example, when the expression {{jsonResponse#.users[*]}} is executed on the "jsonResponse" data returned from the previous task, it returns 3 elements, indicating that this task will run three times.
In the first run, the value within LOOP_VARIABLE will be the following JSON:
{ "id": 1, "name": "user 1", "description": "Description of user 1" } |
Let's assume the desired template to be applied to this JSON is as follows:
{ "test_id":"{{LOOP_VARIABLE#.id}}", "test_name":"{{LOOP_VARIABLE#.name}}", "test_description":"{{LOOP_VARIABLE#.description}}" } |
In that case, the request body to be sent with API Call will be as follows:
{ "test_id": "1", "test_name": "user 1", "test_description": "Description of user 1" } |