Use the JSON. stringify function to Display formatted JSON in HTML. If you have unformatted JSON It will output it in a formatted way. Or Use <pre> tag for showing code itself in HTML page and with JSON.
parse() The JSON. parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);
You know what JSON stands for? JavaScript Object Notation. It makes a pretty good format for objects.
JSON.stringify(obj)
will give you back a string representation of the object.
Most debugger consoles support displaying objects directly. Just use
console.log(obj);
Depending on your debugger this most likely will display the object in the console as a collapsed tree. You can open the tree and inspect the object.
If you want a pretty, multiline JSON with indentation then you can use JSON.stringify
with its 3rd argument:
JSON.stringify(value[, replacer[, space]])
For example:
var obj = {a:1,b:2,c:{d:3, e:4}};
JSON.stringify(obj, null, " ");
or
JSON.stringify(obj, null, 4);
will give you following result:
"{
"a": 1,
"b": 2,
"c": {
"d": 3,
"e": 4
}
}"
In a browser console.log(obj)
does even better job, but in a shell console (node.js) it doesn't.
try console.dir()
instead of console.log()
console.dir(obj);
MDN says console.dir()
is supported by:
to Print JSON parsed object just type console.log( JSON.stringify(data, null, " ") );
and you will get output very clear
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With