What is JSON?

What is JSON Server?

🔍 JSON Syntax Basics

JSON data is written in key-value pairs and can contain:

👉 Example JSON

{
  "name": "Ram Shah",
  "age": 30,
  "isStudent": false,
  "skills": ["JavaScript", "React", "Node.js"],
  "address": {
    "city": "Mumbai",
    "country": "India"
  }
}

🔍 Explanation

⚠️ JSON Rules

How Does JSON Work?

JSON

1. Installation

1. Show how to install JSON Server globally or locally

npm install -g json-server
# OR for local install
npm install json-server --save-dev

2. Create a db.json File

{
  "products": [
    { "id": 1, "name": "iPhone 14", "price": 799 },
    { "id": 2, "name": "Samsung Galaxy S23", "price": 699 }
  ]
}

3. Start JSON Server

json-server --watch db.json --port 5000

API will be available at http://localhost:5000/products.

4. Integrate with React App

JSON is most commonly used:

useEffect(() => {
  fetch('http://localhost:5000/products')
    .then(res => res.json())
    .then(data => setProducts(data));
}, []);

OR using Axios

npm install axios

axios.get('http://localhost:5000/products')
  .then(res => setProducts(res.data));

5.Add / Update / Delete Data

axios.post('http://localhost:5000/products', {
  name: 'OnePlus 12',
  price: 599
});

Benefits of Using JSON Server

Limitations

🔄 JSON vs JavaScript Object

While they look similar, JavaScript Objects and JSON are not the same:

Feature JavaScript Object JSON
Quotes around keys Optional Required (double quotes)
Functions Allowed Not allowed
Comments Allowed Not allowed
Trailing commas Allowed Not allowed

🧾 Final Thoughts

JSON is the standard data format for communication between frontends and backends. It's easy to learn, widely adopted, and forms the foundation for modern web development.