I’m working with Node.js and making API calls to get financial data. The response comes back as JSON in an array format, but I’m having trouble accessing individual fields from the returned data.
When I try to access data by index, I get individual characters instead of the objects I expect. Here’s my current implementation:
const express = require('express');
const bodyParser = require('body-parser');
const https = require('https');
const server = express();
server.use(bodyParser.urlencoded({extended: true}));
server.get('/', function(req, res) {
res.sendFile(__dirname + '/home.html');
});
server.post('/', function(req, res) {
const requestOptions = {
'method': 'GET',
'hostname': 'api.example.com',
'port': null,
'path': '/financial-data/MSFT?key=sample',
'headers': {
'x-api-key': 'your-api-key-here',
'x-api-host': 'financial-api.example.com',
'useQueryString': true
}
};
const apiRequest = https.request(requestOptions, function(apiResponse) {
const dataChunks = [];
if (apiResponse.statusCode === 200) {
console.log('Request successful');
} else {
console.log('Request failed');
}
apiResponse.on('data', function(dataChunk) {
console.log(dataChunk.toString('utf-8')[15]);
dataChunks.push(dataChunk);
});
apiResponse.on('end', function() {
const responseBody = Buffer.concat(dataChunks);
});
});
apiRequest.end();
});
The API returns data like this:
[25 items
0: {35 items
"reportDate": "2023-06-30"
"companySymbol": "MSFT"
"submissionDate": "2023-07-25"
"totalCash": 29263000000
"totalDebt": 47032000000
"currentAssets": 184257000000
"totalAssets": 411976000000
"currentLiabilities": 95082000000
"totalLiabilities": 198298000000
"shareholderEquity": 213678000000
"operatingCashFlow": 87582000000
"freeCashFlow": 71066000000
}, ...]
How can I properly access fields like “totalDebt” or “freeCashFlow” from this response? When I try dataChunk[0] I just get a single character. I thought I could use something like dataChunk[0]["totalDebt"] but that doesn’t work either.