Skip to content

JSON in JavaScript

JSON (JavaScript Object Notation) is a lightweight data interchange format that’s easy for humans to read and write, and easy for machines to parse and generate. It’s widely used for transmitting data between a server and a web application.

What is JSON?

JSON is a text-based format for representing structured data based on JavaScript object syntax. Although it resembles JavaScript object literal syntax, it can be used independently from JavaScript, and many programming environments feature the ability to read and generate JSON.

JSON Syntax Rules

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces {} hold objects
  • Square brackets [] hold arrays
  • Keys must be strings (double quotes)
  • Values can be: string, number, object, array, boolean, or null

JSON Data Types

JSON supports the following data types:

// String
{"name": "John"}
// Number
{"age": 30}
// Boolean
{"isActive": true}
// Null
{"middleName": null}
// Array
{"hobbies": ["reading", "coding", "gaming"]}
// Object
{"address": {"city": "New York", "country": "USA"}}
// Nested structures
{
"person": {
"name": "Alice",
"age": 25,
"skills": ["JavaScript", "Python", "React"]
}
}

JSON.stringify()

The JSON.stringify() method converts a JavaScript object or value to a JSON string.

Basic Usage

const person = {
name: "John Doe",
age: 30,
city: "New York"
};
const jsonString = JSON.stringify(person);
console.log(jsonString);
// Output: {"name":"John Doe","age":30,"city":"New York"}
console.log(typeof jsonString); // "string"

With Replacer Function

You can use a replacer function to filter or transform values:

const user = {
username: "john_doe",
password: "secret123",
email: "john@example.com",
age: 30
};
// Filter out sensitive data
const jsonString = JSON.stringify(user, (key, value) => {
if (key === "password") {
return undefined; // This property will be omitted
}
return value;
});
console.log(jsonString);
// Output: {"username":"john_doe","email":"john@example.com","age":30}

With Space Parameter (Pretty Print)

const data = {
name: "Alice",
skills: ["JavaScript", "React", "Node.js"],
experience: 5
};
// Pretty print with 2 spaces indentation
const prettyJson = JSON.stringify(data, null, 2);
console.log(prettyJson);
/* Output:
{
"name": "Alice",
"skills": [
"JavaScript",
"React",
"Node.js"
],
"experience": 5
}
*/

Handling Special Cases

const specialCases = {
date: new Date(),
func: function() { return "Hello"; },
undef: undefined,
symbol: Symbol("id"),
num: 42
};
console.log(JSON.stringify(specialCases));
// Output: {"date":"2024-01-01T00:00:00.000Z","num":42}
// Note: Functions, undefined, and Symbols are omitted

JSON.parse()

The JSON.parse() method parses a JSON string and constructs the JavaScript value or object described by the string.

Basic Usage

const jsonString = '{"name":"John","age":30,"city":"New York"}';
const person = JSON.parse(jsonString);
console.log(person.name); // "John"
console.log(person.age); // 30
console.log(typeof person); // "object"

Parsing Arrays

const jsonArray = '[1, 2, 3, 4, 5]';
const numbers = JSON.parse(jsonArray);
console.log(numbers); // [1, 2, 3, 4, 5]
console.log(Array.isArray(numbers)); // true

With Reviver Function

You can use a reviver function to transform the resulting object:

const jsonString = '{"name":"Alice","birthDate":"1995-06-15","salary":"50000"}';
const person = JSON.parse(jsonString, (key, value) => {
// Convert date strings to Date objects
if (key === "birthDate") {
return new Date(value);
}
// Convert salary to number
if (key === "salary") {
return Number(value);
}
return value;
});
console.log(person.birthDate instanceof Date); // true
console.log(typeof person.salary); // "number"

Error Handling

Always handle potential parsing errors:

const invalidJson = '{"name": "John", age: 30}'; // Invalid: age key not quoted
try {
const data = JSON.parse(invalidJson);
console.log(data);
} catch (error) {
console.error("Failed to parse JSON:", error.message);
// Output: Failed to parse JSON: Unexpected token a in JSON at position 16
}

Common Use Cases

1. Local Storage

// Storing data
const userData = {
username: "john_doe",
preferences: {
theme: "dark",
language: "en"
}
};
localStorage.setItem("user", JSON.stringify(userData));
// Retrieving data
const storedUser = JSON.parse(localStorage.getItem("user"));
console.log(storedUser.preferences.theme); // "dark"

2. API Communication

// Sending data to server
const postData = {
title: "My Post",
content: "This is the content",
author: "John Doe"
};
fetch("https://api.example.com/posts", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(postData)
});
// Receiving data from server
fetch("https://api.example.com/posts/1")
.then(response => response.json()) // Automatically parses JSON
.then(data => console.log(data));

3. Deep Cloning Objects

const original = {
name: "Alice",
skills: ["JavaScript", "Python"],
address: {
city: "Boston"
}
};
// Quick deep clone (with limitations)
const clone = JSON.parse(JSON.stringify(original));
clone.skills.push("React");
clone.address.city = "New York";
console.log(original.skills); // ["JavaScript", "Python"] - unchanged
console.log(original.address.city); // "Boston" - unchanged

Note: This method has limitations - it doesn’t handle functions, undefined, Date objects, or circular references properly.

4. Configuration Files

// config.json content
const configJson = `{
"apiUrl": "https://api.example.com",
"timeout": 5000,
"retryAttempts": 3,
"features": {
"darkMode": true,
"notifications": false
}
}`;
const config = JSON.parse(configJson);
console.log(config.apiUrl); // "https://api.example.com"
console.log(config.features.darkMode); // true

Best Practices

  1. Always validate JSON before parsing

    function safeJsonParse(str) {
    try {
    return JSON.parse(str);
    } catch (e) {
    return null;
    }
    }
  2. Handle circular references

    const obj = { name: "John" };
    obj.self = obj; // Circular reference
    // This will throw an error
    // JSON.stringify(obj); // TypeError: Converting circular structure to JSON
    // Solution: Use a custom replacer
    const seen = new WeakSet();
    const jsonString = JSON.stringify(obj, (key, value) => {
    if (typeof value === "object" && value !== null) {
    if (seen.has(value)) {
    return "[Circular]";
    }
    seen.add(value);
    }
    return value;
    });
  3. Use type checking after parsing

    const data = JSON.parse(jsonString);
    if (data && typeof data === "object") {
    // Safe to use data
    }

Common Pitfalls

  1. Trailing commas are not allowed in JSON

    // Invalid JSON
    const invalid = '{"name": "John",}';
    // Valid JSON
    const valid = '{"name": "John"}';
  2. Single quotes are not valid

    // Invalid JSON
    const invalid = "{'name': 'John'}";
    // Valid JSON
    const valid = '{"name": "John"}';
  3. Undefined values are omitted

    const obj = { name: "John", age: undefined };
    console.log(JSON.stringify(obj)); // {"name":"John"}

Summary

  • JSON is a lightweight data format for data exchange
  • JSON.stringify() converts JavaScript objects to JSON strings
  • JSON.parse() converts JSON strings to JavaScript objects
  • Always use proper error handling when parsing JSON
  • JSON is commonly used with APIs and local storage
  • Remember JSON’s limitations with certain data types like functions and undefined