2

Fetching json data through http.request, I am able to receive data in chunks, which I push into an array. when the end of the request is signaled, I concatenate the array using buffer.concat, and then json.parse this object, which gives me a string. I then must json.parse this string again to get a json object. Why am I having to json.parse twice? Is there a better way to achieve a valid json object? Here is my code:

// requires Node.js http module
var http = require('http');

var options = {
    "method" : "GET",
    "hostname" : "localhost",
    "port" : 3000,
    "path" : `/get`

};

var req = http.request(options, function (res) {
    var chunks = [];
    console.log(res.statusCode);
    res.on("data", function (chunk) {
        // add each element to the array 'chunks'
        chunks.push(chunk);
    });
    // adding a useless comment...
    res.on("end", function () {

        // concatenate the array
        // iterating through this object spits out numbers (ascii byte values?)
        var jsonObj1 = Buffer.concat(chunks);
        console.log(typeof(jsonObj1));    

        // json.parse # 1
        // iterating through this string spits out one character per line
        var jsonObj = JSON.parse(jsonObj1);
        console.log(typeof(jsonObj));

        // json.parse # 2    
        // finally... an actual json object
        var jsonObj2 = JSON.parse(jsonObj);
        console.log(typeof(jsonObj2));

        for (var key in jsonObj2) {
            if (jsonObj2.hasOwnProperty(key)) {
                var val = jsonObj2[key];
                console.log(val);
            }
        }
    });
});

req.on('error', (e) => {
    console.error(`problem with request: ${e.message}`);
  });

req.end();
Estex
  • 93
  • 1
  • 7
  • the server is serving encoded json as a json-encoded string? – dandavis Jun 25 '18 at 20:15
  • 1
    If you have to parse it twice, it means the server encoded it twice. – Barmar Jun 25 '18 at 20:30
  • @Barmar, what makes a node server to encode the data twice? I am currently working on a project with a heavy use of streams and I never got this problem. – planet_hunter Jun 25 '18 at 20:58
  • @planet_hunter Calling `JSON.stringify()` twice. Or maybe you're calling a response method that automatically stringifies, but you called stringify on the parameter first. – Barmar Jun 25 '18 at 20:59
  • @Barmar, Thanks! So I tried to access a file with stringified JSON using `request`. Now I tried `JSON.parse()` and got the error `SyntaxError: Unexpected token n in JSON at position 3` which means the stringified JSON stored in the Buffer couldn't get converted. However when I did `Buffer.toString()`, I got the stringified JSON in the console. Not sure if I did something wrong but it's not as simple as I thought. – planet_hunter Jun 25 '18 at 21:17
  • @planet_hunter The problem is in the `/get` script on the server. If you post that we can try to figure out where the double-encoding is happening. – Barmar Jun 25 '18 at 21:20
  • Well the question is asked by @Estex :) I was just trying to understand as I ended up giving a wrong answer! :) – planet_hunter Jun 25 '18 at 21:22
  • The server was encoding it twice using stringify. – Estex Jun 26 '18 at 16:29

0 Answers0