D DevBrainBox

JavaScript JSON

JS

JSON, or JavaScript Object Notation, is a lightweight data-interchange format. It's a way to organize and transport data that's easy for humans to read and write, and easy for machines to parse and generate. Despite its name, JSON is language-independent and is used by many programming languages, not just JavaScript.

What is JSON?

  • JSON stands for JavaScript Object Notation.
  • It is a lightweight data format used to store and exchange data.
  • It is easy for humans to read and write, and easy for machines to parse and generate.

It is often used to send data between a client (browser) and a server.

JSON Syntax

JSON is written in key-value pairs inside .

  • Keys are always strings (enclosed in double quotes " ").
  • Values can be: string, number, object, array, boolean, or null.
{
  "name": "Alice",
  "age": 25,
  "isStudent": false,
  "hobbies": ["reading", "gaming", "coding"],
  "address": {
    "city": "New York",
    "zip": "10001"
  }
}

JSON in JavaScript

JSON is very similar to JavaScript objects, but there are differences:

  • JSON keys must be in double quotes.
  • JSON does not support functions or comments.

Converting between JSON and JavaScript objects

1️. JSON to JavaScript Object

Use JSON.parse() to convert JSON string → JavaScript object.

const jsonString = '{"name":"Alice","age":25}';
const obj = JSON.parse(jsonString);

console.log(obj.name); // Alice
console.log(obj.age);  // 25

2️. JavaScript Object to JSON

Use JSON.stringify() to convert JavaScript object → JSON string.

const person = {
  name: "Bob",
  age: 30
};

const jsonStr = JSON.stringify(person);
console.log(jsonStr); // {"name":"Bob","age":30}

On this page