Get the specific element by parsing JSON formatted Response
3 min readJan 29, 2024
How to get a specific element from the Response body?
Do not hurry, do it step by step. Here, the response is in JSON format. Beforehand, we’re parsing JSON formatted response to retrieve the Javascript object. Here, we want to get the “Active”: true.
{
"Response": {
"MetaInfo": {
"Timestamp": "2021-07-23T07:41:38.720+0000"
},
"Data": [
{
"_type": "SearchResultsAddress",
"DataId": 2912,
"Results": [
{
"Relevance": 0,
"Address": {
"State": "Colorado",
"Street": "239 Monares Ln",
"City": "Monte Vista",
"PostalCode": "81144",
"Active": false
}
},
{
"Relevance": 1,
"Address": {
"State": "Colorado",
"Street": "206 Lyell St",
"City": "Erie",
"PostalCode": "80516",
"Active": true
}
}
]
}
]
}
}
// We are parsing Json string to get JS object to get the required items.
// response is displayed below.
const response = pm.response.json();
// This object only has one property and which is called "Response"
console.log(response);
// "Data" is an array and it has only one 1 element, so index is zero
console.log(response.Response.Data[0]);
// Retrieve the "Results" property from the "Data".
// "Results" is also an array. "Results" has 2 objects.
// To reach the "Active": true, the second object should be used.
// Second object is being index 1.
console.log(response.Response.Data[0].Results[1]);
// we retrieving address from the second object
console.log(response.Response.Data[0].Results[1].Address);
// in the end, get the "Active" property from an "Address" object.
// so, Active propety's value is true as shown below.
console.log(response.Response.Data[0].Results[1].Address.Active);
Have a good day to all!
We often think that great achievements require great actions. However, it is surprising the difference a small improvement can make over time. If you improve by 1 percent every day for 1 year, you will be 37 times better by the end of the year. So, keep working!