I have heard of sites that prettify/beautify JSON. What does it actually mean?
Asked
Active
Viewed 980 times
2
-
Indentation? Proper use of quotes? – Matijs Apr 15 '11 at 07:39
4 Answers
7
This means a more human readable version of it. E.g. the following is valid json, but not well readable:
{"outcome" : "success", "result" : {"name" : "messaging-sockets", "default-interface" : "external", "include" : [], "socket-binding" : {"messaging" : {"name" : "messaging", "interface" : null, "port" : 5445, "fixed-port" : null, "multicast-address" : null, "multicast-port" : null}, "messaging-throughput" : {"name" : "messaging-throughput", "interface" : null, "port" : 5455, "fixed-port" : null, "multicast-address" : null, "multicast-port" : null}}}, "compensating-operation" : null}
After prettyfying this could look like this:
{
"outcome":"success",
"result":{
"name":"messaging-sockets",
"default-interface":"external",
"include":[
],
"socket-binding":{
"messaging":{
"name":"messaging",
"interface":null,
"port":5445,
"fixed-port":null,
"multicast-address":null,
"multicast-port":null
},
"messaging-throughput":{
"name":"messaging-throughput",
"interface":null,
"port":5455,
"fixed-port":null,
"multicast-address":null,
"multicast-port":null
}
}
},
"compensating-operation":null
}

John Gietzen
- 48,783
- 32
- 145
- 190

Heiko Rupp
- 30,426
- 13
- 82
- 119
1
It makes your code pretty, i.e. it indents it, makes sure stuff is aligned in a similar manner, all parenthesis are placed in a similar manner, etc.
Example
var obj = {apple: {red: 5, green: 1}, bananas: 9}; //JS object
var str = JSON.stringify(obj, null, 4); // spacing level 4, or instead of 4 you can write "\t" for tabulator
//The third argument from stringify function enables pretty printing and sets the spacing to use.
console.log(str); //now you can see well pretty printed JSON string in console
But if you want to do it on yourself then you can use the second parameter as a function. More about JSON.stringify
function you can find here.
A lot of examples how to pretty print JSON string.

Bharata
- 13,509
- 6
- 36
- 50

Daniel Hilgarth
- 171,043
- 40
- 335
- 443
-
Thanks for reply, please provide a small example of ugly and preety JSON :) – Shamim Hafiz - MSFT Apr 15 '11 at 07:42
0
Like @Heiko Rupp mentioned, it makes easier to read. Most JSON files are already prettified. If you want to prettify your own JSON formatted variable, you can do it like so:
import json
variable = {'a': 1, 'b': 2, 'c': 3}
print(variable)
# returns:
{'a': 1, 'b': 2, 'c': 3}
# prettify
print(json.dumps(variable, indent=4, sort_keys=True))
# returns:
{
"a": 1,
"b": 2,
"c": 3
}

G. Sliepen
- 7,637
- 1
- 15
- 31

hyukkyulee
- 1,024
- 1
- 12
- 17