| Version | Release Date |
|---|---|
| Original | 2006-07-28 |
| Data type | Examples of valid JSON |
|---|---|
| ### String | "apple" "\u00c4pfel\n" "" |
| ### Number | 3 1.4 -1.5e3 |
| ### Boolean | true false |
| ### Null | null |
{
"id": 1,
"name": "A wooden door",
"price": 12.50,
"tags": ["home", "wooden"]
}
["home", "wooden"]
[
1,
2,
[3, 4, 5, 6],
{
"id": 1,
"name": "A wooden door",
"price": 12.50,
"tags": ["home", "wooden"]
}
]
{ "key1": "value1", "key2": "value2", ... }
Keys must be valid strings, thus surrounded by double
quotation marks. Values can be JSON objects, numbers, strings,
arrays, or one of the following 'literal names': false, null,
or true. In a key-value pair, the key is seperated from the
value by a colon. Multiple key-value-pairs are separated by
commas.{ "key2": "value2", "key1": "value1", ... }
To sum it all up, this is an example of a valid JSON Object :
{
"image": {
"width": 800,
"height": 600,
"title": "View from 15th Floor",
"thumbnail": {
"url": "http://www.example.com/image/481989943",
"height": 125,
"width": 100
},
"visible": true,
"ids": [116, 943, 234, 38793]
}
}
{ "colors" : [ "red", "green", "blue" ] }
JSON Arrays can also contain any valid JSON element, including
objects, as in this example of an array with 2 objects (taken
from the RFC document):
[
{
"precision": "zip",
"Latitude": 37.7668,
"Longitude": -122.3959,
"Address": "",
"City": "SAN FRANCISCO",
"State": "CA",
"Zip": "94107",
"Country": "US"
},
{
"precision": "zip",
"Latitude": 37.371991,
"Longitude": -122.026020,
"Address": "",
"City": "SUNNYVALE",
"State": "CA",
"Zip": "94085",
"Country": "US"
}
]
They can also contain elements with mixed types, for example:
[
"red",
51,
true,
null,
{
"state": "complete"
}
]
A common mistake when writing JSON arrays (and objects) is to
leave a trailing comma after the last element. This is common
pattern in many languages, but unfortunately isn't valid in
JSON. For example, the following array is invalid:
[
1,
2,
]
To make this valid, you would need to remove the comma after
the last element, turning it into:
[
1,
2
]
{
a: 1,
b: 2,
c: 3
}
Adding a comma after 3 will produce a synax error.
[
1,
2
]
You must take extra care if you need to reorder the items.
{
a: 1,
b: 2,
c: 3
d: 4
}
Since trailing commas are not allowed, it is easy to forget to
append one before adding a new value (in this case after
3).
{
"//": "comment",
"data": 1
}
{
"data": "comment",
"data": 1
}
The second data entry will overwrite the comment in most
parsers.
object fruit :{seed:{}, endocarp:{},flesh:{}}
This also implies that anything within the seed object should
be property of seed, say: colour, ..
JSON.parse('"bar of foo"')
// "bar of foo" (type string)
JSON.parse("true")
// true (type boolean)
JSON.parse("1")
// 1 (type number)
JSON.parse("[1,2,3]")
// [1, 2, 3] (type array)
JSON.parse('{"foo":"bar"}')
// {foo: "bar"} (type object)
JSON.parse("null")
// null (type object)
Invalid strings will throw a JavaScript error
JSON.parse('{foo:"bar"}')
// Uncaught SyntaxError: Unexpected token f in JSON at position 1
JSON.parse("[1,2,3,]")
// Uncaught SyntaxError: Unexpected token ] in JSON at position 7
JSON.parse("undefined")
// Uncaught SyntaxError: Unexpected token u in JSON at position 0
The JSON.parse method includes an optional reviver function
which can limit or modify the parse
| Param | Details |
|---|---|
| Object (Object) | The JSON object |
| Replacer |
(Function | Array
|
| Space | (Number | string - optional) Amount of white space in the JSON |
var JSONObject = {
stringProp: 'stringProp',
booleanProp: false,
intProp: 8
}
var JSONString = JSON.stringify(JSONObject);
console.log(JSONString);
/* output
* {"stringProp":"stringProp","booleanProp":false,"intProp":8}
*/
Stringify with filter
var JSONObject = {
stringProp: 'stringProp',
booleanProp: false,
intProp: 8
}
var JSONString = JSON.stringify(JSONObject, ['intProp']);
console.log(JSONString);
/* output
* {"intProp":8}
*/
Stringify with white-space
var JSONObject = {
stringProp: 'stringProp',
booleanProp: false,
intProp: 8
}
var JSONString = JSON.stringify(JSONObject, null, 2);
console.log(JSONString);
/* output:
* {
* "stringProp": "stringProp",
* "booleanProp": false,
* "intProp": 8
* }
*/