← Back to index

The JSON object and the JSON.Parse() function

Created: 2015-12-10 16:50  |  Updated: 2015-12-10 16:54  |  Source: desktop.mac

24.3The JSON Object

The JSON object is the %JSON% intrinsic object and the initial value of the JSON property of the global object. The JSON object is a single ordinary object that contains two functions, parse and stringify, that are used to parse and construct JSON texts. The JSON Data Interchange Format is defined in ECMA-404. The JSON interchange format used in this specification is exactly that described by ECMA-404.

Conforming implementations of JSON.parse and JSON.stringify must support the exact interchange format described in the ECMA-404 specification without any deletions or extensions to the format.

The value of the [[Prototype]] internal slot of the JSON object is the intrinsic object %ObjectPrototype% (19.1.3). The value of the [[Extensible]] internal slot of the JSON object is set to true.

The JSON object does not have a [[Construct]] internal method; it is not possible to use the JSON object as a constructor with the new operator.

The JSON object does not have a [[Call]] internal method; it is not possible to invoke the JSON object as a function.

24.3.1JSON.parse ( text [ , reviver ] )

The parse function parses a JSON text (a JSON-formatted String) and produces an ECMAScript value. The JSON format is a subset of the syntax for ECMAScript literals, Array Initializers and Object Initializers. After parsing, JSON objects are realized as ECMAScript objects. JSON arrays are realized as ECMAScript Array instances. JSON strings, numbers, booleans, and null are realized as ECMAScript Strings, Numbers, Booleans, and null.

The optional reviver parameter is a function that takes two parameters, key and value. It can filter and transform the results. It is called with each of the key/value pairs produced by the parse, and its return value is used instead of the original value. If it returns what it received, the structure is not modified. If it returnsundefined then the property is deleted from the result.

  1. Let JText be ToString(text).
  2. ReturnIfAbrupt(JText).
  3. Parse JText interpreted as UTF-16 encoded Unicode points (6.1.4) as a JSON text as specified in 
    ECMA-404. Throw a SyntaxError exception if JText is not a valid JSON text as defined in that specification.
  4. Let scriptText be the result of concatenating "("JText, and ");".
  5. Let completion be the result of parsing and evaluating scriptText as if it was the source text of an ECMAScript Script. but using the alternative definition of DoubleStringCharacter provided below. The extended PropertyDefinitionEvaluation semantics defined in B.3.1 must not be used during the evaluation.
  6. Let unfiltered be completion.[[value]].
  7. Assertunfiltered will be either a primitive value or an object that is defined by either an ArrayLiteral or an ObjectLiteral.
  8. If IsCallable(reviver) is true, then
    1. Let root be ObjectCreate(%ObjectPrototype%).
    2. Let rootName be the empty String.
    3. Let status be CreateDataProperty(rootrootNameunfiltered).
    4. Assertstatus is true.
    5. Return InternalizeJSONProperty(rootrootName).
  9. Else
    1. Return unfiltered.

JSON allows Unicode code units 0x2028 (LINE SEPARATOR) and 0x2029 (PARAGRAPH SEPARATOR) to directly appear in String literals without using an escape sequence. This is enabled by using the following alternative definition of DoubleStringCharacter when parsing scriptText in step 5:

DoubleStringCharacter ::
SourceCharacter but not one of " or \ or U+0000 through U+001F
\ EscapeSequence

NOTEThe syntax of a valid JSON text is a subset of the ECMAScript PrimaryExpression syntax. Hence a valid JSON text is also a valid PrimaryExpression. Step 3 above verifies that JText conforms to that subset. When scriptText is parsed and evaluated as a Script the result will be either a String, Number, Boolean, or Null primitive value or an Object defined as if by an ArrayLiteral orObjectLiteral.

http://www.ecma-international.org/ecma-262/6.0/index.html#sec-json-object