Path: blob/main/files/en-us/web/javascript/reference/global_objects/json/index.md
6520 views
------{{JSRef}}
The JSON object contains methods for parsing JavaScript Object Notation ({{glossary("JSON")}}) and converting values to JSON. It can't be called or constructed.
Description
Unlike most global objects, JSON is not a constructor. You cannot use it with a new operator or invoke the JSON object as a function. All properties and methods of JSON are static (just like the {{jsxref("Math")}} object).
JavaScript and JSON differences
JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and null. It is based upon JavaScript syntax, but is distinct from JavaScript: most of JavaScript is not JSON. For example:
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.
NaNandInfinityare unsupported.
Any JSON text is a valid JavaScript expression, but only after the JSON superset revision. Before the revision, U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR are allowed in string literals and property keys in JSON; but the same use in JavaScript string literals is a {{jsxref("SyntaxError")}}.
Other differences include allowing only double-quoted strings and no support for {{jsxref("undefined")}} or comments. For those who wish to use a more human-friendly configuration format based on JSON, there is JSON5, used by the Babel compiler, and the more commonly used YAML.
The same text may represent different values in JavaScript object literals vs. JSON as well. For more information, see Object literal syntax vs. JSON.
Full JSON grammar
Valid JSON syntax is formally defined by the following grammar, expressed in ABNF, and copied from IETF JSON standard (RFC):
Insignificant {{glossary("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.
Static properties
JSON[@@toStringTag]: The initial value of the
@@toStringTagproperty is the string"JSON". This property is used in {{jsxref("Object.prototype.toString()")}}.
Static methods
{{jsxref("JSON.parse()")}}
: Parse a piece of string text as JSON, optionally transforming the produced value and its properties, and return the value.
{{jsxref("JSON.stringify()")}}
: Return a JSON string corresponding to the specified value, optionally including only certain properties or replacing property values in a user-defined manner.
Examples
Example JSON
You can use the {{jsxref("JSON.parse()")}} method to convert the above JSON string into a JavaScript object:
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
{{jsxref("Date.prototype.toJSON()")}}
JSON Diff checker