Categories
javascript json object-notation

What is the difference between JSON and Object Literal Notation?

247

Can someone tell me what is the main difference between a JavaScript object defined by using Object Literal Notation and JSON object?

According to a JavaScript book it says this is an object defined by using Object Notation:

var anObject = {
    property1 : true,
    showMessage : function (msg) { alert(msg) }
};

Why isn’t it a JSON object in this case? Just because it is not defined by using quotation marks?

2

283

Lets clarify first what JSON actually is. JSON is a textual, language-independent data-exchange format, much like XML, CSV or YAML.

Data can be stored in many ways, but if it should be stored in a text file and be readable by a computer, it needs to follow some structure. JSON is one of the many formats that define such a structure.

Such formats are typically language-independent, meaning they can be processed by Java, Python, JavaScript, PHP, you name it.

In contrast, JavaScript is a programming language. Of course JavaScript also provides a way to define/describe data, but the syntax is very specific to JavaScript.

As a counter example, Python has the concept of tuples, their syntax is (x, y). JavaScript doesn’t have something like this.


Lets look at the syntactical differences between JSON and JavaScript object literals.

JSON has the following syntactical constraints:

  • Object keys must be strings (i.e. a character sequence enclosed in double quotes ").
  • The values can be either:
    • a string
    • a number
    • an (JSON) object
    • an array
    • true
    • false
    • null
  • Duplicate keys ({"foo":"bar","foo":"baz"}) produce undefined, implementation-specific results; the JSON specification specifically does not define their semantics

In JavaScript, object literals can have

  • String literals, number literals or identifier names as keys (since ES6, keys can now also be computed, which introduces yet another syntax).
  • The values can be any valid JavaScript expression, including function definitions and undefined.
  • Duplicate keys produce defined, specified results (in loose mode, the latter definition replaces the former; in strict mode, it’s an error).

Knowing that, just by looking at the syntax, your example is not JSON because of two reasons:

  1. Your keys are not strings (literals). They are identifier names.
  2. You cannot assign a function as a value to a “JSON object” (because JSON doesn’t define any syntax for functions).

But most importantly, to repeat my explanation from the beginning: You are in a JavaScript context. You define a JavaScript object. If any, a “JSON object” can only be contained in a string:

 var obj = {foo: 42}; // creates a JavaScript object (this is *not* JSON)
 var json = '{"foo": 452}'; // creates a string containing JSON

That is, if you’re writing JavaScript source code, and not dealing with a string, you’re not dealing with JSON. Maybe you received the data as JSON (e.g., via ajax or reading from a file), but once you or a library you’re using has parsed it, it’s not JSON anymore.


Only because object literals and JSON look similar, it does not mean that you can name them interchangeably. See also There’s no such thing as a “JSON Object”.

10

  • 10

    Also note that JSON is a subset of Object Literal Notation

    May 25, 2010 at 14:14

  • 15

    @SeanKinsey: Except that it isn’t: timelessrepo.com/json-isnt-a-javascript-subset

    – mpen

    Mar 22, 2013 at 18:09

  • 1

    Might be worth noting that typically you’d expect a JavaScript object literal in a context where comments are legal, and the JSON spec doesn’t allow for comments (see this post.

    Sep 5, 2014 at 22:32


  • keys in object literal are always strings, despite you use “” or not.

    Mar 24, 2016 at 18:11


  • 2

    @overexchange: “keys in object literal are always strings” You are mixing two things here, but I can’t blame you because I also didn’t draw a clear line here. You have to distinguish between an object literal and an object value. A literal is the character sequence you write in the source code. The value is what’s created by interpreting the source code. The object literal (syntax) allows you do use identifier names, string literals or number literals. You are correct that at runtime, those are all converted to strings (but we have symbols now as well).

    Mar 24, 2016 at 18:17

47

JSON has a much more limited syntax including:

  • Key values must be quoted
  • Strings must be quoted with " and not '
  • You have a more limited range of values (e.g. no functions allowed)

2

  • 1

    Liked this “no function Allowed”.

    – Karan Kaw

    Feb 20, 2017 at 8:30

  • Comments are not allowed either. For questionable reasons. (Heard them questioned few times.) This is the main difference i”d say.

    – user7610

    Oct 25, 2017 at 19:08


18

There is really no such thing as a “JSON Object”.

The JSON spec is a syntax for encoding data as a string. What people call a “JSON Object” ( in javascript ) is really just an ordinary javascript object that has (probably) been de-serialized from a valid JSON string, and can be easily re-serialized as a valid JSON string. This generally means that it contains only data ( and not functions ). It also means that there are no dates, because JSON does not have a date type ( probably the most painful thing about JSON 😉

Furthermore, (side-rant…) when people talk about a “JSON Object”, they almost always mean data that has the “curly-braces” at the top-level. This corresponds nicely to a javascript object. However, the JSON spec does not require that there be a single “curly-braces” object at the top-level of a JSON string. It is perfectly valid JSON to have a list at the top-level, or even to have just a single value. So, while every “JSON Object” corresponds to valid JSON, not all valid JSON strings correspond to what we would call a “JSON Object”! ( because the string could represent a list or an atomic value )

1

  • 5

    There is an error in your answer: it is invalid JSON to have an atomic value at the top level. JSON allows the top to be either an object or an array, but nothing else. RFC4627, the standard, describes JSON’s grammar as JSON-text = object / array.

    Sep 18, 2013 at 15:52