Skip to main content

JSON Syntax

JSON is a lightweight data-interchange format. It is easy for humans to read and write.

It is also easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language Standard ECMA-262 3rd Edition - December 1999.

JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

Let's see how to write JSON data in JavaScript.

  • Data is represented as key/value pairs, separated by a colon :.
  • Key/value pairs are separated by commas ,.
  • Data is enclosed in curly braces {} to create an object.
  • Arrays are enclosed in square brackets [].
  • Strings are enclosed in double quotes "".
  • Numbers can be integer or floating-point and can be written without quotes.
  • Boolean values are represented as true or false.
  • Null values are represented as null.

Here's an basic example of JSON syntax:

{
"name": "John Doe",
"age": 30,
"isEmployed": true,
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip": "12345"
},
"hobbies": ["reading", "hiking", "traveling"]
}

In this example

  • The JSON data represents an object with the properties name, age, isEmployed, address, and hobbies.
  • The address property is itself an object, and the hobbies property is an array.

Difference between JSON and JavaScript object

JavaScript objects and JSON are related but not the same thing. While they have some similarities, there are also some key differences.

Here are some key differences between JavaScript objects and JSON:

  • Syntax: JavaScript objects are defined using JavaScript syntax, while JSON is defined using a string format that follows specific rules.
// JavaScript object
const person = {
name: "John Doe",
age: 30,
isEmployed: true,
};

// JSON
{
"name": "John Doe",
"age": 30,
"isEmployed": true,
}
  • Quotes: In JavaScript, property names can be unquoted if they are valid JavaScript identifiers. In JSON, property names must always be double-quoted strings.
// JavaScript object
const person = {
name: "John Doe",
age: 30,
isEmployed: true,
};

// JSON
{
"name": "John Doe",
"age": 30,
"isEmployed": true,
}
  • Functions: JSON does not support functions or methods, while JavaScript objects can contain functions as properties.
// JavaScript object
const person = {
name: "John Doe",
age: 30,
isEmployed: true,
sayHello: function () {
console.log("Hello");
},
};

// JSON
{
"name": "John Doe",
"age": 30,
"isEmployed": true,
}
  • Comments: JSON does not allow comments, while JavaScript objects can have comments within the code.
  • Data Interchange: JSON is primarily used as a data interchange format, while JavaScript objects are used to represent data structures within a JavaScript program.

  • Data Types: JSON supports a limited set of data types, including strings, numbers, booleans, null, arrays, and objects. JavaScript objects can represent any data type, including functions and classes.