What is JSON?

What is JSON?

JSON stands for JavaScript Object Notation.

JSON is a lightweight format for storing and transporting data. The JSON is similar to JavaScript Objects syntax wise but JSON is text-only. It is used primarily to transmit data between a server and web application, as an alternative to XML.

The JSON format is syntactically identical to the code for creating JavaScript objects and JavaScript can easily convert JSON code into native JavaScript objects with its inbuilt string methods.

Since JSON format is text only, it can easily be sent tot and form a server, and used as a data format by programming language.

JSON Code example

"name" : "John"

The JSON format is identical to JavaScript Objects. JSON data consists of key/value pairs similar to JavaScript object properties. The key and values are written in double quotes separated by a colon. JSON objects are written inside curly braces. Just like JavaScript, objects can contain multiple name/value pairs.

{
  "name": "John"
}

The JSON names require double quotes. JavaScript variables names don’t require.

{
  name: "John"
}

JSON is built in two structures:

  1. A collection of name/values pairs. In various languages, this is realized as an object, or list.
  2. An ordered list of values. This is realized as an array, or vector.

The JSON values must be one of the following data type:

  • a string
  • a number
  • an object
  • an array
  • a boolean
  • null

Values of JSON cannot be one of the following data types:

  • a function
  • a date
  • undefined

Common JSON Example

Convert Object to JSON String

Converting the JavaScript to JSON String with JSON.stringify().

let car = {
    name: "Ferrai",
    color: "red"
};
let jsontxt = JSON.stringify(car);
console.log(jsontxt);

// {"name": "Ferrari", "color", "red"}

Convert JSON to JavaScript Object

Parsing the JSON Data with JSON.parse(), the data becomes JavaScript Object.

let car = `{
	"name": "Ferrai",
    "color": "red"
}`;
let obj = JSON.parse(car);
console.log(obj.name);

// Ferrari

Thanks for scrolling & Share this blog, if you have liked it. You can read our other blogs here. Follow us on Instagram & Github.

Other blog are:

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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

17 + twenty =