I’m running my javascript code with node.js and am using the request-promise library to generate https requests which return a json blob. So far, I’ve managed to send the request correctly–and I do receive the desired json object in the response, but I’m only seeing the keys, not the values. I want to extract the value for a key called “call_count”. The problem I’m facing is the peculiar structure of the request-promise syntax, which appears to be preventing me from doing what I want to do.
Here’s what the JSON response looks like when I run my query in Postman:
{
"metric_data": {
"from": "2021-12-12T23:56:12+00:00",
"to": "2021-12-13T00:26:12+00:00",
"metrics_not_found": [],
"metrics_found": [
"Mobile/Crash/All"
],
"metrics": [
{
"name": "Mobile/Crash/All",
"timeslices": [
{
"from": "2021-12-12T23:53:00+00:00",
"to": "2021-12-13T00:23:00+00:00",
"values": {
"call_count": 0
}
}
]
}
]
}
}
And here’s my code that generates the request:
const rp = require ('request-promise');
let options = {
method: 'GET',
uri: 'https://api.crashCatcher.com/v2/mobile_applications/myAppID/metrics/data.json?names=Mobile/Crash/All&values[]=call_count&summarize=true',
headers: {
'User-Agent': 'request-promise',
'X-Api-Key': 'MY API key goes here'
},
json: true // Automatically parses the JSON string in the response
};
rp(options).then(body => console.log(body)).catch(err => console.log(err.name, err.statusCode));
Currently, when I run this code, here’s what I’m getting back:
{ metric_data:
{ from: '2021-12-13T00:22:04+00:00',
to: '2021-12-13T00:52:04+00:00',
metrics_not_found: [],
metrics_found: [ 'Mobile/Crash/All' ],
metrics: [ [Object] ] } }
Now, I know I’m getting a JSON object back, and I know I need to do something like:
var myJson = JSON.parse(*body*);
And then something like:
console.log(myJson.metric_data.metrics[0].timeslices[0].values.call_count);
But when using the request-promise syntax, I don’t know how to correctly assign what’s coming back in my response body to a variable that I can then JSON.parse and extract the needed call_count value from.