JSON Introduction
JSON stands for JavaScript Object Notation. It is a lightweight, text-based data format used to store and transport information. While it originated from JavaScript, almost every programming language today (Python, PHP, Java, C#, etc.) can read and write JSON data.
The JSON Format
Think of JSON as a way to "package" data into a string so it can be sent over the internet easily. Here is what a typical JSON packet looks like:
Serialized JSON Example
{
"id": 101,
"name": "RedoHub",
"isLearning": true,
"lessons": ["HTML", "JS", "JSON"]
}
Why is JSON so Popular?
- Lightweight: It uses very few characters, making transmission fast.
- Readable: It is easy for humans to read and write.
- Self-Describing: The keys tell you what the data represents.
- Language Independent: It works with virtually every modern language.
JSON Syntax Rules
To be valid JSON, you must follow strict syntax rules:
- Data is in name/value pairs (Key: Value).
- Data is separated by commas.
- Curly braces
{} hold objects.
- Square brackets
[] hold arrays.
- Keys must always be strings wrapped in double quotes.
Common Mistake: In standard JavaScript objects, quotes around keys are optional. In JSON, double quotes " " are strictly mandatory for keys.
JSON vs. XML
Before JSON, XML was the standard for data exchange. JSON has largely replaced it because it is much simpler to parse and write.
| Comparison |
XML |
JSON |
| Closing tags |
Required (verbose) |
None |
| Parsing |
Uses an XML parser |
Native JSON.parse() |
| Arrays |
Can use them |
Uses them extensively |
JSON Data Types
A JSON value can be one of the following:
- a string (in double quotes)
- a number (integer or floating point)
- an object (JSON object)
- an array
- a boolean (true/false)
- null
Note: JSON values cannot be functions, dates, or undefined.
Key Points to Remember
- JSON is a lightweight data interchange format.
- It is purely text, so it can be sent to any server.
- Syntax is based on JavaScript Objects.
- Keys must have double quotes.
- It is faster and easier to work with than XML.