Answer the question
In order to leave comments, you need to log in
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": []
}
}
{ 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: [] } }
Answer the question
In order to leave comments, you need to log in
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.
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.
Have a look at this library, it might work. archive.dojotoolkit.org/nightly/dojotoolkit/dojox/gfx/demos/beautify.html
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question