JSON and its uses and differences between JSON and JavaScript

This is a short introduction to JSON. In this blog, you will study what JSON is, why it is used and what are some of the differences between JSON and JavaScript. You’ll study the major things about JSON in the next blog. So let’s study some minor things first.

  • JSON stands for JavaScript Object Notation.
  • It is a syntax for storing and exchanging data.
  • It is a lightweight data-interchange format.
  • It is “self-describing” and easy to understand.
  • It is language independent.

Since the JSON format is text only, it can easily be sent to and from a server and used as a data format by any programming language.
JavaScript has a built-in function to convert a string, written in JSON format, into native JavaScript objects:
JSON.parse()
So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.

Exchanging Data

The data can only be text when exchanging it between a browser and a server. JSON is text, and we can convert any JavaScript object into JSON, and send JSON to the server. We can also convert any JSON received from the server into JavaScript objects. In this way, we can work with the data as JavaScript objects, with no complicated parsing and translations.

Sending Data

If you have data stored in a JavaScript object, you can convert the object into JSON, and send it to a server.
Example:
var myObj = {name: "Chris", age: 31, city: "Jaipur"};
var myJSON = JSON.stringify(myObj);
window.location = "demo_json.php?x=" + myJSON;

Receiving Data

If you receive data in JSON format, you can convert it into a JavaScript object.
Example:
var myJSON = '{"name":"Chris", "age":31, "city":"Jaipur"}';
var myObj = JSON.parse(myJSON);
document.getElementById("demo").innerHTML = myObj.name;

Storing Data

The data has to be a certain format when storing, and regardless of where you choose to store it, the text is always one of the legal formats.
JSON makes it possible to store JavaScript objects as text.
Example:
Storing data in local storage:
// Storing data:
myObj = {name: "Chris", age: 25, city: "Jaipur"};
myJSON = JSON.stringify(myObj);
localStorage.setItem("testJSON", myJSON);

// Retrieving data:
text = localStorage.getItem("testJSON");
obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.name;

JSON Syntax

The JSON syntax is a subset of the JavaScript syntax.

JSON Syntax Rules

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays

JSON Data – A Name and a Value

JSON data is written as name/value pairs.
A name/value pair consists of a field name (in double quotes), followed by a colon (:), followed by a value.
Example:
“name”:”Chris”
Note: JSON names require double quotes (” “).

JSON – Evaluates to JavaScript Objects

The JSON format is almost identical to JavaScript objects. In JSON, keys must be string, written with double-quotes like, {"name":"Chris"}whereas, in JavaScript, keys can be string, number or identifier name as, {name:"Chris"}.

JSON Values

In JSON, values must be a string, a number, an object (JSON object), an array, a boolean or null, whereas, in JavaScript, values can be a string, a number, an object (JSON object), an array, a boolean, a function, a date, undefined or null.

In JSON, string values must be written in double-quotes like, {"name":"Chris"}, whereas, in JavaScript, you can write string values with any, double or single quotes like, {name:'Chris'}.

Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript.
With JavaScript, you can create an object and assign data to it. The same way JavaScript objects can be used as JSON, JavaScript arrays can also be used as JSON.

JSON Files

  • The file type for JSON files is “.json”
  • The MIME type for JSON text is “application/json”.

Leave a Reply

Your email address will not be published. Required fields are marked *