Skip to content

JSON

This lesson explains JSON in JavaScript with beginner-friendly examples, practical use cases, and clear best practices.

JavaScript JSON Overview

JSON (JavaScript Object Notation) is a lightweight data-interchange format used to exchange information between clients and servers. JSON is easy for humans to read and write, and easy for machines to parse and generate, making it the standard format for REST APIs, web services, configuration files, and modern web applications.

JavaScript provides the built-in JSON object with methods such as JSON.parse() and JSON.stringify() to convert between JSON strings and JavaScript objects.

JSON Feature Description Common Usage
JSON Object Stores data as key-value pairs. API responses.
JSON Array Stores ordered collections. Product lists and users.
JSON.parse() Converts JSON string into JavaScript object. Reading API data.
JSON.stringify() Converts JavaScript object into JSON string. Sending data to APIs.
Nested JSON Supports objects inside objects and arrays. Complex application data.

JavaScript JSON Example

const jsonText = '{
  "name": "John",
  "age": 30,
  "city": "Dallas"
}';

const user = JSON.parse(jsonText);

console.log(user.name);

const jsonString = JSON.stringify(user);

console.log(jsonString);

This example converts a JSON string into a JavaScript object using JSON.parse(), then converts the object back into a JSON string using JSON.stringify().

Top 10 JavaScript JSON Examples

The following examples demonstrate common JSON operations used in APIs, local storage, configuration files, and data exchange.

# Example Syntax
1 Create JSON Object '{"name":"John"}'
2 Parse JSON JSON.parse(jsonString)
3 Convert to JSON JSON.stringify(user)
4 Nested JSON Object '{"address":{"city":"Dallas"}}'
5 JSON Array '["Apple","Orange","Mango"]'
6 Pretty Print JSON JSON.stringify(user, null, 2)
7 Store in Local Storage localStorage.setItem("user", JSON.stringify(user))
8 Read from Local Storage JSON.parse(localStorage.getItem("user"))
9 Fetch API JSON const data = await response.json();
10 Convert Array to JSON JSON.stringify(products)

JavaScript JSON Methods

Method Description Example Output
JSON.parse() Converts JSON string into JavaScript object. JSON.parse('{"name":"John"}') { name: "John" }
JSON.stringify() Converts JavaScript object into JSON string. JSON.stringify({ name: "John" }) '{"name":"John"}'
JSON.stringify(value, replacer) Filters properties while converting. JSON.stringify(user, ["name"]) '{"name":"John"}'
JSON.stringify(value, null, 2) Formats JSON with indentation. JSON.stringify(user, null, 2) Pretty formatted JSON
JSON.parse(text, reviver) Transforms values while parsing. JSON.parse(text, (key, value) => value) Parsed object

Best Practices

  • Use JSON for exchanging data between applications.
  • Always validate JSON before parsing.
  • Wrap JSON.parse() inside try...catch to handle invalid JSON.
  • Use meaningful property names.
  • Keep JSON data lightweight.
  • Use JSON.stringify() before storing objects in Local Storage.
  • Avoid storing functions or undefined values in JSON.
  • Use pretty formatting for debugging.

Common Mistakes

  • Using single quotes inside JSON data.
  • Adding trailing commas.
  • Including comments inside JSON.
  • Trying to store functions in JSON.
  • Parsing invalid JSON without error handling.
  • Confusing JavaScript objects with JSON strings.
  • Using undefined values, which are ignored by JSON.

Key Takeaways

  • JSON is the standard format for exchanging data.
  • JSON.parse() converts JSON strings into JavaScript objects.
  • JSON.stringify() converts JavaScript objects into JSON strings.
  • JSON supports objects, arrays, strings, numbers, booleans, and null.
  • JSON does not support functions, comments, or undefined values.
  • JSON is widely used in REST APIs, Local Storage, databases, and configuration files.

Pro Tip

Remember that JSON is a text format, while JavaScript objects are native language objects. Use JSON.parse() when reading JSON data and JSON.stringify() when sending or storing JavaScript objects.