How to extract specific fields from API response data in Node.js

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.

You’re trying to access the data before it’s fully loaded. The dataChunk in your ‘data’ event handler is just a Buffer with raw bytes - not parsed JSON yet. You need to collect all chunks first, then parse the complete response. Here’s the fix: apiResponse.on(‘end’, function() { const responseBody = Buffer.concat(dataChunks).toString(‘utf-8’); const financialData = JSON.parse(responseBody); // Now you can access the fields properly console.log(financialData[0].totalDebt); console.log(financialData[0].freeCashFlow); }); Ditch the parsing attempt from the ‘data’ event handler - that’s working with incomplete data. Network responses come in multiple chunks, so wait until everything arrives before parsing the JSON.

You’re treating the response buffer like it’s already parsed JSON. When you do dataChunk.toString('utf-8')[15], you’re just grabbing individual characters from the raw string response, not object properties. I hit this same issue building a trading dashboard last year. Fix is simple - collect all chunks, convert to string, then parse as JSON. After your Buffer.concat line, add: const jsonData = JSON.parse(responseBody.toString('utf-8')); const firstRecord = jsonData[0]; console.log(firstRecord.totalDebt, firstRecord.freeCashFlow); This converts the complete response into a JavaScript object array you can navigate normally. Don’t try accessing data in the ‘data’ event handler since you’re working with incomplete chunks there.

You’re parsing the JSON while it’s still streaming in chunks. Wait for all the data first, then use JSON.parse(). Move your parsing code to the ‘end’ event after you’ve concatenated all the chunks.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.