Format JSON in more readable way


#1

When I try to output JSON result using JSON.stringify I get result below. There is escape double quote tags and some text minimized and not visible.

Is it possible to output JSON like this in more user friendly way?


#2

The easiest way to do this is to just do JSON.parse(that_string). Alternatively, have the request library treat the contents as JSON if you are expecting JSON. For example, I believe you are getting that result from reading the raw response, where you could alternatively be getting the JSON value instead:

Instead of:

const got = require("got");
(await got.post('http://v2.convertapi.com/token/create?secret=nop')).body;

Do:

const got = require("got");
(await got.post('http://v2.convertapi.com/token/create?secret=nop', { json: true })).body;

Now the object will show up the way you want it to.

– Why can’t we just show your JSON the right way? Because RunKit, just like your REPL or console.log has no way to differentiate between a string and a “JSON string”. In the same way that it has no way of knowing that “**hello**” is markdown. Its just a string. We would have to try to parse every string as JSON, and if it succeeds, then show it as such – which still wouldn’t be correct all the time, since “5” is a valid JSON string but it would be confusing to show that as the NUMBER 5 and not the string “5”.