What is JSON?

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

Why Use JSON?

JSON is commonly used in web applications to transmit data between a server and a web client. It helps in sending structured data in a way that is both easy to understand and process.

Basic Structure of JSON

A JSON object is written in a key/value pair format. Here’s how a simple JSON object looks:

{
"name": "John",
"age": 15,
"isStudent": true
}

In this example:

  • "name": "John" - This key-value pair indicates that the name is John.
  • "age": 15 - This indicates the person is 15 years old.
  • "isStudent": true - This shows that John is a student.

Data Types in JSON

JSON can hold various data types:

  • String: Text enclosed in double quotes, e.g., "Hello".
  • Number: Numeric values, e.g., 42 or 3.14.
  • Boolean: True or false values.
  • Array: An ordered list of values enclosed in brackets, e.g., ["apple", "banana", "cherry"].
  • Object: A collection of key/value pairs enclosed in braces, as shown above.

How to Use JSON

To use JSON data in web applications, you usually fetch it from a server and parse it using JavaScript. Here’s a simple example:

fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data));

This code fetches the JSON data from a URL and logs it to the console.

Conclusion

JSON is a simple yet powerful way to handle data. Understanding JSON is essential for anyone interested in programming, especially in web development.