How to display API responses in bot messages?
We have implemented JSON Path to address JSON objects and arrays.
I assume you know how to make an API Call. I will skip to the responses part.
Edit the block where you want the response to be displayed
The format of the message would be {{blockName.JSONObject}}
For example,
You have made the API Call in a Block named “APICall“
The response you are getting is a JSON object
{
"botId": 123,
"categoryName": "Vegetables",
"description": "Fresh green vegetables",
"isActive": true,
"isPopular": true,
"tags": ["food","natural"]
}
and you want to use the value of categoryName in your message then you will write {{APICall.categoryName}}
.
Similarly, the below should work –
{{APICall.botId}}
{{APICall.description}}
{{APICall.isActive}}
{{APICall.isPopular}}
If you have a property or key in the object, whose value is an array(e.g. tags), then you can access the value by using child notations for the array i.e. [index]. For example, the “tags” property has value as an array. For accessing an individual value, use
{{APICall.tags[0]}}
For complex JSON objects, you could first evaluate your JSON at https://jsonpath.com/ and then use the same address in the botbaba message blocks.
In case, if the call is made in APICall block, and the API’s response is array data, similar to
[
{
"firstName": "John"
},
{
"lastName": "Doe",
"phoneNumber": [0000000000, 1111111111]
}
]
Here, In this case, the response data can be captured by using the “data” key in conjunction with the name of the block in which the API request got made. For example, for the response shown above, to use the property “firstName”, write {{APICall.data[0].firstName}}
.
Similarly, the below should work:-
{{APICall.data[1].lastName}}
{{APICall.data[1].phoneNumber[0]}}