How can we pretty-print JSON using JavaScript ?
In javascript JSON.stringify() method converts into value of JSON string.
To display JSON in an easy-to-read format for indentation and whitespace, with perhaps even colors / font-styles / etc.
If we need syntax highlighting, we might use some regex magic like so:
We have an object to be printed as pretty . Only valid JSON string process to be occur, we want to convert it to an object first:
It is create a JSON object from the string, and then converts to a string using JSON stringify's as pretty to be print.
We can use console.dir()
, it is expand as console.log(util.inspect())
.
It uses syntax-highlighting, smart indentation, removes quotes from keys and the output gets as pretty.
and for the command line:
We use this code for debugging :
It works well: