Extract data from the server's JSON response and immediately use it in scripts.
For example, in the response from the server, the following JSON was received:
{
"ok": true,
"result": {
"user": {
"id": 421122300,
"is_bot": false,
"first_name": "Noah",
"last_name": "Smith",
"username": "noahsmith",
"language_code": "en"
},
"status": "creator"
}
}
It is necessary to extract the value of the first_name field. Let's look at how to do this below.
To get the field value from the server response, define the full path to this value. For example, for the first_name field:
body.result.user.first_name
Make up the address of the field:
body — this is always the body of the response.result.user object, which contains the user's data.first_name.
falsevalues are treated as an empty string, which can cause errors. Make sure you check such data before using.
When the response contains arrays, data extraction is slightly different. Let's say the server returns the following response:
{
"calls": [
{"phone": "31000112211", "variables": {"firstName": "Liam"}},
{"phone": "31000223344", "variables": {"firstName": "Noah"}}
]
}
Here you need to get the value of the firstName field for the second element of the calls array.
The full address of the field:
body.calls.1.variables.firstName
Create an address:
body — the basis of all queries.calls — this is an array of objects.variables field.firstName.Apply the received address in your scenario:
userFirstName.body.result.user.first_name.