Deep Tech Point
first stop in your tech adventure

What’s the difference between JSON and JavaScript object?

March 4, 2021 | Javascript

In this article, you’re going to learn the difference between JSON and JavaScript object.

What is JSON?

JSON stands for JavaScript Object Notation and represents a minimal, readable format for structuring data. JSON serves as an alternative to XML and is used primarily to transmit data between a server and web application. For example, some websites use JSON to save and organize the content they create with CMS.

JavaScript inspired the syntax of JSON, but there are some differences between them. So, let’s take a look at them.

Unlike Javascript object literals, in JSON quotes are mandatory – all keys must be quoted:

{ "brand": "BMW" } // JSON

var car = { brand: "BMW" }; // Object literal

So, why are quotes mandatory in JSON?

In JavaScript, more precisely in ECMAScript 3rd edition, the usage of reserved words as property names is not allowed:

var name = { for: "some value" }; // "for" is the reserved word and its usage as a property name will cause SyntaxError in ES3

So, when you apply quotes in a property name, that won’t cause any problems, like so:

var name = { "for": "some value" };

The quotes are mandatory in JSON because of compatibility reasons.

Can I use single or double quotes in JSON, just like in JavaScript?

No, you can not use single or double quotes in JSON. The JSON standard requires double quotes and will not accept single quotes, nor will the parser. In JavaScript, however, you can use single or double quotes interchangeably (although this is not a good practice – you should decide on the use of either single or double quotes).

{ "brand": 'BMW' } // this is not valid in JSON, you have to use double quotes, like so: 

{ "brand": "BMW" } // valid JSON

What about values of data types in JSON vs. JavaScript?

In JSON, values must be one of the following data types:

JSON values cannot be one of the following data types:

If compared to JavaScript – JavaScript can have seven data types, six of them being primitive (number, string, boolean, undefined, null, and symbol) and one complex (object).

Numbers in JSON vs. JavaScript

In JavaScript you can use hexadecimal literals (for example 0xF), or octal literals (where leading is zero can only use digits 0-7, for example 010). Compared to many other programming languages, JavaScript does not define different types of numbers (for example integers, short, long, floating-point). So, JavaScript has only one type of number, which can be written with or without decimals, like so:

var x = 7.16;    // an example of a number with decimals
var y = 7;       // an example of a number without decimals

In JSON the grammar of numbers is a bit different – in JSON you can use only decimal literals.

{ "color": 0x00FF00 } // in JSON that example is not valid

In conclusion, compared to JavaScript JSON has a much more limited syntax. You must always quote key values. You must always quote strings with double quotes and not single quotes. And, you have a more limited range of values, for example, in JSON there are no functions or dates allowed.