--- title: JSON slug: Web/JavaScript/Reference/Global_Objects/JSON tags: - JSON - JavaScript - NeedsTranslation - Object - Reference - TopicStub - polyfill translation_of: Web/JavaScript/Reference/Global_Objects/JSON ---
The JSON
object contains methods for parsing JavaScript Object Notation ({{glossary("JSON")}}) and converting values to JSON. It can't be called or constructed, and aside from its two method properties it has no interesting functionality of its own.
JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and {{jsxref("null")}}. It is based upon JavaScript syntax but is distinct from it: some JavaScript is not JSON, and some JSON is not JavaScript. See also JSON: The JavaScript subset that isn't.
JavaScript type | JSON differences |
---|---|
Objects and Arrays | Property names must be double-quoted strings; trailing commas are forbidden. |
Numbers | Leading zeros are prohibited; a decimal point must be followed by at least one digit. |
Strings |
Only a limited sets of characters may be escaped; certain control characters are prohibited; the Unicode line separator (U+2028) and paragraph separator (U+2029) characters are permitted; strings must be double-quoted. See the following example where {{jsxref("JSON.parse()")}} works fine and a {{jsxref("SyntaxError")}} is thrown when evaluating the code as JavaScript: var code = '"\u2028\u2029"'; JSON.parse(code); // works fine eval(code); // fails |
The full JSON syntax is as follows:
JSON = null or true or false or JSONNumber or JSONString or JSONObject or JSONArray JSONNumber = - PositiveNumber or PositiveNumber PositiveNumber = DecimalNumber or DecimalNumber . Digits or DecimalNumber . Digits ExponentPart or DecimalNumber ExponentPart DecimalNumber = 0 or OneToNine Digits ExponentPart = e Exponent or E Exponent Exponent = Digits or + Digits or - Digits Digits = Digit or Digits Digit Digit = 0 through 9 OneToNine = 1 through 9 JSONString = "" or " StringCharacters " StringCharacters = StringCharacter or StringCharacters StringCharacter StringCharacter = any character except " or \ or U+0000 through U+001F or EscapeSequence EscapeSequence = \" or \/ or \\ or \b or \f or \n or \r or \t or \u HexDigit HexDigit HexDigit HexDigit HexDigit = 0 through 9 or A through F or a through f JSONObject = { } or { Members } Members = JSONString : JSON or Members , JSONString : JSON JSONArray = [ ] or [ ArrayElements ] ArrayElements = JSON or ArrayElements , JSON
Insignificant whitespace may be present anywhere except within a JSONNumber
(numbers must contain no whitespace) or JSONString
(where it is interpreted as the corresponding character in the string, or would cause an error). The tab character (U+0009), carriage return (U+000D), line feed (U+000A), and space (U+0020) characters are the only valid whitespace characters.
The JSON
object is not supported in older browsers. You can work around this by inserting the following code at the beginning of your scripts, allowing use of JSON
object in implementations which do not natively support it (like Internet Explorer 6).
The following algorithm is an imitation of the native JSON
object:
if (!window.JSON) { window.JSON = { parse: function(sJSON) { return eval('(' + sJSON + ')'); }, stringify: function(vContent) { if (vContent instanceof Object) { var sOutput = ''; if (vContent.constructor === Array) { for (var nId = 0; nId < vContent.length; sOutput += this.stringify(vContent[nId]) + ',', nId++); return '[' + sOutput.substr(0, sOutput.length - 1) + ']'; } if (vContent.toString !== Object.prototype.toString) { return '"' + vContent.toString().replace(/"/g, '\\$&') + '"'; } for (var sProp in vContent) { sOutput += '"' + sProp.replace(/"/g, '\\$&') + '":' + this.stringify(vContent[sProp]) + ','; } return '{' + sOutput.substr(0, sOutput.length - 1) + '}'; } return typeof vContent === 'string' ? '"' + vContent.replace(/"/g, '\\$&') + '"' : String(vContent); } }; }
More complex well-known polyfills for the JSON
object are JSON2 and JSON3.
Specification | Status | Comment |
---|---|---|
{{SpecName('ES5.1', '#sec-15.12', 'JSON')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-json-object', 'JSON')}} | {{Spec2('ES6')}} |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatGeckoDesktop("1.9.1")}} | {{CompatIE("8.0")}} | {{CompatOpera("10.5")}} | {{CompatSafari("4.0")}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoMobile("1.0")}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Based on Kangax's compat table.
JSON