0

Using Node.js and am fairly new to the software. However this is something that bothered me from my vanilla front-end days. I have a simple html.get stream

https.get(url, (res)=>{
    res.setEncoding('utf8');
    let body = '';
    res.on('data', (data)=>{
        body += data;
    });

    res.on('end', ()=>{
        body = JSON.parse(body);
        //console.log(typeof body);
        callback(body);
    })
})

with the callback function just returning the file to my webpage

 function callback(body){
    res.send(body);
}

The problem that I have with this is that the file that I send to my server, i.e. var = body , is that the file is... not easily readable. I'm not sure if that makes complete sense so this might be a better explanation:

This is what I want:

{
"title": "Google Custom Search - lectures",
"totalResults": "970000000",
"searchTerms": "lectures",
"count": 10,
"startIndex": 11,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"safe": "off",
"cx": "[REDACTED]"
}

with all the whitespaces intact. This is the what the URL for the get gives. However after receiving it and concatenating/parsing it with my algorithm I get:

{"title":"Google Custom Search - lectures","totalResults":"970`0000","searchTerms":"lectures","count":10,"startIndex":11,"inputEncoding":"utf8","outputEncoding":"utf8","safe":"off","cx":"[REDACTED]"}]}

without any white-spaces or anything. It just comes out as 'blocks of texts'. I (HOPE) I did something wrong and there is a way to preserve the original format when I use the html.get call.

My original plan was to use some 'regex magic' to format it to become more readable, but there should be another way I hope considering the original URL has it formatted it properly. I'm dealing with a big JSON file, so a more readable format would be nice. I'm not sure if I explained my scenario perfectly, this is my first Stack question, but if its not please let me know. Thanks!

pandey
  • 1
  • check this out: https://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript – Gil Apr 18 '18 at 05:53
  • That was the thread I was just on. I suppose 'regex magic' is the one true answer for this, huh. – pandey Apr 18 '18 at 05:59
  • Look here: https://stackoverflow.com/questions/19696240/proper-way-to-return-json-using-node-or-express – Vincent Schöttke Apr 18 '18 at 06:01
  • JSON.stringify(body, null, 4) or so should do the trick - in my opinion - but it still makes doesn't work properly. Maybe, when I'm concatenating something goes wrong. I'm going to go to sleep, but maybe it will click when I wake up tomorrow. – pandey Apr 18 '18 at 06:14

1 Answers1

1

You can write a simple function to format the JSON the way you want. You might find you want a different format for logging vs. web page etc. This wraps the JSON.stringify method (well documented here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify and here:
https://www.w3schools.com/js/js_json_stringify.asp.

var data = {"title":"Google Custom Search - lectures","totalResults":"970`0000","searchTerms":"lectures","count":10,"startIndex":11,"inputEncoding":"utf8","outputEncoding":"utf8","safe":"off","cx":"[REDACTED]"};

function prettyPrintJSON(data, space = 2) {
    return JSON.stringify(data, null, space);
}
console.log(prettyPrintJSON(data));

The output would look like so:

{
  "title": "Google Custom Search - lectures",
  "totalResults": "970`0000",
  "searchTerms": "lectures",
  "count": 10,
  "startIndex": 11,
  "inputEncoding": "utf8",
  "outputEncoding": "utf8",
  "safe": "off",
  "cx": "[REDACTED]"
}

Here's a JSFiddle example:

https://jsfiddle.net/uovtaxq3/3/

Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • 1
    Thanks! This isn't exactly what I needed, but it helped me figure out my problem. The white space from the get was being written into my parsed JSON as and \n. Just changing res.send() to res.write() managed to fix it. – pandey Apr 18 '18 at 13:04