M
M
Mithgol2013-01-24 12:09:07
JavaScript
Mithgol, 2013-01-24 12:09:07

Is there a javascript for JSON output that comes with nice indentation and appropriate newlines?

The usual output of the JSON.stringify() function is a solid line - something like this:
{"status":"Hub", "number":68, "title":"Lucky Carrier", "location":"Kiev", "sysop":"Pavel Gulchouck", "flags":{"IBN":[], "INA":["fido.happy.kiev.ua"], "IMI":["[email protected]"] , "MO":[], "XA":[], "CM":[], "PING":[]}}
(Spaces after commas are added for ease of viewing, but in fact there are none.)
It's compact , which makes the resulting JSON ideal for being sent over the Web, for example.
But it will be very difficult for a person to read such a conclusion, especially if there is several times more data,
Meanwhile, sometimes it becomes necessary to read JSON for debugging purposes, and sometimes you want to store settings in a file that would be equally convenient for reading both JSON.parse() and a person. Therefore, I wonder if there is such a javascript that is able to output JSON in a beautiful human-readable form, with nice (and creating readability) newlines and indented spaces - something like this:

{
"status":   "Hub",
"number":   68,
"title":    "Lucky Carrier",
"location": "Kiev",
"sysop":    "Pavel Gulchouck",
"flags": {
   "IBN":  [],
   "INA":  ["fido.happy.kiev.ua"],
   "IMI":  ["[email protected]"],
   "MO":   [],
   "XA":   [],
   "CM":   [],
   "PING": []
}
}

The desired ones are therefore:
  • Jump to a new line at the beginning of the object (after the opening curly brace), at the end of the object (before the closing curly brace), between the fields of the object (after the comma).
     
  • Indents before object field names corresponding to the level of nesting of objects into each other.
     
  • Padding before the values ​​to compensate for differences in the length of their names so that the left edges of the values ​​line up under each other.

Even so, the final output should still be valid JSON.
Here is a clear example of how not to do it: you cannot replace the JSON.stringify() call with the console.log(require('util').inspect()) call in the Node engine . The result of the work, of course, will then be beautiful:
{ status: 'Hub',
  number: 68,
  title: 'Lucky Carrier',
  location: 'Kiev',
  sysop: 'Pavel Gulchouck',
  flags:
   { IBN: [],
     INA: [ 'fido.happy.kiev.ua' ],
     IMI: [ '[email protected]' ],
     MO: [],
     XA: [],
     CM: [],
     PING: [] } }

This output does not start a newline before and after curly braces, nor does it have indentation before values, but even half of the above conveniences already do a lot to achieve the desired human readability.
However, it is easy to see, unfortunately, that require('util').inspect() removes the quotes around the names of the object's fields - and replaces the quotes around the values ​​with single quotes (apostrophes) instead of double quotes (like the inch sign).
This means that the result is no longer JSON.
This is sad.
I ask therefore: is there a remedy better than this? Is there any open source javascript that can output JSON with nice indentation and appropriate newlines, but without breaking JSON rules anywhere?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Mithgol, 2013-01-24
@Mithgol

Well, I found the answer myself.
The JSON.stringify() function has an optional third parameter, and if you pass a string of spaces there, it will be used as an indent.
Thank you all for your participation.

N
Nikolay, 2013-01-24
@Piterski

I don’t remember exactly and I could be wrong, but doesn’t FireBug have a function to view the response in json format? For debugging, that's it.

S
skyboy, 2013-01-24
@skyboy

Have a look at this library, it might work. archive.dojotoolkit.org/nightly/dojotoolkit/dojox/gfx/demos/beautify.html

K
Konstantin T, 2013-01-24
@RooTooZ

Check out this link

N
Nikolai Turnaviotov, 2013-01-26
@foxmuldercp

By the way, how many nodes are alive in Kyiv? :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question