Are all JSON objects also valid JavaScript objects?
Asked Answered
F

2

31

The JSON standard defines objects in one way and the ECMAScript (JavaScript) standard defines it in another.

It is often said that JSON objects are a subset of JavaScript objects, is this true?

Is every JSON object also a valid JavaScript object?

Federicofedirko answered 20/5, 2014 at 6:7 Comment(0)
D
38

Update 2019: the answer is now YES as of this proposal and JavaScript versions following ECMAScript 2019 (including) will be proper supersets.


TL;DR

The answer is "no". There are cases when JSON object won't be valid for JavaScript. JSON is NOT a JavaScript subset.

"Little" difference

JSON

That is: due to JSON specification, you can safely use such characters, as U+2028 in any string. It is a unicode whitespace character. Not control or other special character.

enter image description here

JavaScript

Well, now in JavaScript. ECMA-262 has a little difference in its definition of strings. In section 7.8.4 there is a thing, that string can contain all things except quote, a backslash or a line terminator. Now what's line terminator? It's in section 7.3 :

  • \u000A - Line Feed
  • \u000D - Carriage Return
  • \u2028 - Line separator
  • \u2029 - Paragraph separator

As you can see, in JavaScript symbols U+2028 and U+2029 are not allowed.

This is a sample, but since we have at least one case of difference, it's well-enough to realize that answer is no

Image source & full description: timelessrepo

Discotheque answered 20/5, 2014 at 7:10 Comment(0)
M
3

First, some precaution should be taken when using the term "JSON object":

If you want, "JSON object" can refer to the object that a JSON text represents. Even the JSON specification defines what "object" means:

An object is an unordered collection of zero or more name/value pairs

This is just an intent, as JSON itself is not a processing language: it does not parse text into objects.

Not all JSON texts represent objects (e.g. they can represent string or number literals), so speaking of "JSON object" does have some additional value: it would be short for "a JSON text that represents an object".

It's like saying "email notification". Email is a communication mechanism, and one particular email can represent a message to you. It might represent a notification of something, but does not have to be.

JSON versus JavaScript object literals

While "JSON objects" might be a valid term, it should not be used for JavaScript objects. JSON can be used in many language platforms, so the historic connection with JavaScript should really be laid aside.

JavaScript object literals have other syntax rules than JSON, so they should not be confused. For instance:

  • JSON requires that strings are wrapped in double quotes, while JavaScript object literals may have non-quoted property names, and single quoted strings;
  • JSON is restricted to a few data types, while JavaScript object literals may include other data types and notations, like regular expression literals, template literals, functions, ...etc;
  • JSON does not allow empty elements in array literals, while JavaScript literals do (e.g. [1, , 2]);
  • JSON allows U+2028 and U+2029 characters in strings. Before EcmaScript2019, these characters needed to be escaped in JavaScript. This difference is taken away with EcmaScript2019;
Misadvise answered 21/7, 2017 at 9:34 Comment(3)
A JSON object is well defined in the spec I linked to. JSON is not any more "text" than JavaScript (or XML, or C is) - JSON clearly defines what properties are (in the spec) and how they behave. What it does not define is any behavior of those properties (since it's a data exchange format). For example the JSON object {"x": 1e99999} is a valid JavaScript object in terms of syntax but out of bounds for JavaScript semantically (since doubles can't hold numbers that big).Federicofedirko
I realize people sometimes mistake JSON objects for JS ones and vice versa since it's the same general notation but that was not the question. The question was specifically if there is a JSON object that when parsed as a JavaScript object is invalid (that is, proof that JavaScript's actual object notation is not a "superset" of the JSON object notation). Out of all your points - the only one demonstrating this is the last - which the last answer covers. Thanks for posting an answer though!Federicofedirko
I completely agree with you. I added this introduction on the term "JSON object" because it seems useful for visitors. I regularly refer to this useful question, like here. In that context, it seemed useful to touch on some aspects that go beyond your question.Misadvise

© 2022 - 2024 — McMap. All rights reserved.