I am using Jira API's to get data on single tickets. I have successfully setup a http GET request to the server and can display the data to the console however I ideally need to get certain properties from the data which is in JSON format.
When I try to read the properties I just get undefined.
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk); // This displays the JSON
console.log('endSTATUS: ' + chunk.id); // This shows up undefined
});
The data is in this format from jira API for reference. The first console log in the res successfully displays all the data from the chunk. The second one is:
endSTATUS: undefined
Try to get the body after the data stream finish. Like this:
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
// Data reception is done, do whatever with it!
var parsed = JSON.parse(body);
console.log('endSTATUS: ' + parsed.id);
});
Make sure you are parsing the response data as JSON. I think you may want something like var data = JSON.parse(chunk);
, and reference the chunk data as data.value
.
res.on('data', function (chunk) {
var data = JSON.parse(chunk);
console.log('BODY: ' + data);
console.log('endSTATUS: ' + data.id);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With