This free online JSON formatter lets you chose your indentation level and creates a collapsible/expandable tree with structure highlights in colors
JSON stands for "JavaScript Object Notation" and is pronounced "Jason" (like in the Friday the 13th movies). It's meant to be a human-readable and compact solution to represent a complex data structure and facilitate data-interchange between systems.
There are tons of reasons why you would want to use JSON:
There are just a few rules that you need to remember:
Example:
{
"anObject": {
"numericProperty": -122,
"stringProperty": "An offensive \" is problematic",
 "nullProperty": null,
"booleanProperty": true,
"dateProperty": "2011-09-23"
},
"arrayOfObjects": [
{
"item": 1
 },
{
"item": 2
},
{
"item": 3
}
],
"arrayOfIntegers": [
1,
2,
3,
4,
5
]
}
Because JSON derives from JavaScript, you can parse a JSON string simply by invoking the eval() function. The JSON string needs to be wrapped by parenthesis, else it will not work! This is the #1 problem when programmers first start to manipulate JSON strings. That being said, DON'T do this!
Example using the 'dangerous' eval():
<script>
// A valid json string
var someJsonString = '{"someProperty":"someValue"}';
// jsonObject will contain a valid JavaScript object
var jsonObject = eval('(' + someJsonString + ')');
// Will display the string 'someValue'
alert(jsonObject.someProperty);
</script>
A better and more secure way of parsing a JSON string is to make use of JSON.parse(). The eval() function leaves the door open to all JS expressions potentially creating side effects or security issues, whereas JSON.parse() limits itself to just parsing JSON. JSON.parse() is available natively in most recent browsers.
Example using JSON.parse():
<script>
// A valid json string
var someJsonString = '{"someProperty":"someValue"}';
// jsonObject will contain a valid JavaScript object
var jsonObject = JSON.parse(someJsonString);
// Will display the string 'someValue'
alert(jsonObject.someProperty);
</script>
If you want to create a JSON string representation of your JavaScript object, make use of the JSON.stringify() function.
Example of using JSON.stringify:
<script>
// A valid json string
var someObject = {};
someObject.someProperty = "someValue";
// jsonString now contains a JSON string representation of someObject
var jsonString = JSON.stringify(someObject);
// Will display the string '{"someProperty":"someValue"}'
alert(jsonString);
</script>
You can also create JavaScript objects using the JSON syntax directly in your code.
Example of creating a JavaScript object using 'JSON' syntax:
<script>
// jsonObject is a valid JavaScript object that can be used on the fly
var jsonObject = { someProperty : "someValue" };
// Will display the string 'someValue'
alert(jsonObject.someProperty);
</script>