JavaScript JSON  

JavaScript JSON

What is JSON?

 JSON is a format for storing and transporting data. JSON is often used when data is sent from a server to a web page.

  • JSON stands for JavaScript Object Notation
  • JSON is a lightweight data-interchange format
  • JSON is language independent *
  • JSON is "self-describing" and easy to understand

* The JSON syntax is derived from JavaScript object notation syntax, but the JSON format is text only. The code for reading and generating JSON data can be written in any programming language.


JSON Objects

JSON objects are written inside curly braces.

Just like in JavaScript, objects can contain multiple name/value pairs:

{"firstName":"Deepak", "lastName":"Kumar"}

JSON Arrays

JSON arrays are written inside square brackets.

Just like in JavaScript, an array can contain objects:

  {"firstName":"Deepak", "lastName":"Chahar"},
  {"firstName":"Meenakshi", "lastName":"Nirman"},
  {"firstName":"Susheel", "lastName":"Kumar"}

In the example above, the object "employees" is an array. It contains three objects.

Each object is a record of a person (with a first name and the last name).

Converting a JSON Text to a JavaScript Object

A common use of JSON is to read data from a web server and display the data on a web page.

For simplicity, this can be demonstrated using a string as input.

Then, use the JavaScript built-in function JSON.parse() to convert the string into a JavaScript object:

var obj = JSON.parse(text);


<p id="demo"></p>

document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;

Download free E-book of JAVASCRIPT

Learn Programming for Free

Join Programmers Community on Telegram

Talk with Experienced Programmers

Just drop a message, we will solve your queries