π JSON Tutorial β Your Comprehensive Guide to JSON
π What is JSON?
JSON (JavaScript Object Notation) is a lightweight, text-based format for data interchange. Think of it as the language your browser and servers use to chatβand it’s human-readable.
β
Language-independent
β
Easy to parse
β
Widely used in web APIs and configurations
π JSON Syntax Rules
- π Data is in key/value pairs
- π Keys are double-quoted strings
- π’ Values can be string, number, object, array, true/false, null
- β No trailing commas allowed
- β Whitespace (spaces, line breaks) doesnβt matter
Example:
{
"name": "Alice",
"age": 30,
"isMember": true
}
π’ JSON Data Types
Type | Example |
---|---|
string | "Hello" |
number | 25 , 3.14 , -5 |
object | {"a":1, "b":2} |
array | [1,2,3] |
boolean | true , false |
null | null |
πΎ Reading & Writing JSON
πͺ In JavaScript:
const text = '{"city":"Paris","temp":18}';
const obj = JSON.parse(text);
console.log(obj.city); // Paris
const str = JSON.stringify(obj);
console.log(str); // '{"city":"Paris","temp":18}'
π JSON in JavaScript
JSON.parse()
converts JSON string β JS objectJSON.stringify()
converts JS object β JSON string
β Perfect for storing configs, passing data between client/server
π JSON in Other Languages
- Python:
import json
βjson.load()
,json.dumps()
- Java: Jackson / Gson libraries handle JSON
- PHP:
json_encode()
,json_decode()
JSON is universal π
βοΈ Working with JSON APIs
- Use fetch (JS):
fetch('https://api.example.com/data') .then(res => res.json()) .then(data => console.log(data));
- Include appropriate headers (
Content-Type: application/json
) - Handle errors:
if (!res.ok) throw new Error('Server error');
π οΈ JSON Validation & Tools
- β JSONLint β Online validator
- π§© Editor plugins: VSCode, Sublime
- βοΈ CLI tools:
jq
,ajv
,jsonschema
π JSON Schema
Define structure, types, constraints:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"age": { "type": "number", "minimum": 0 }
},
"required": ["age"]
}
π Official JSON Schema
π JSON Security Best Practices
- β οΈ Avoid parsing untrusted JSON
- β Validate against schema
- π‘οΈ Use HTTPS
- π§― Limit size to prevent DoS
β Real-World JSON Use Cases
- π¦ Config files (e.g.
package.json
) - π RESTful APIs
- π Data storage (lightweight DBs)
- π Inter-service communication
π Best Practices
- βοΈ Use meaningful keys
- π§Ή Keep it consistent (same format)
- π¦ Avoid nesting too deeply
- π Document structure
- π§° Use tools to auto-generate schema
π Conclusion
JSON is your go-to format for data exchangeβsimple, flexible, and language-agnostic. From saving settings to talking to APIs, mastering JSON is essential for any dev. Start writing clean JSON, validate it, and integrate it in your next app. π
β FAQs
Q1: Is JSON case-sensitive?
π
°οΈ Yesβkeys and values must match exactly.
Q2: Can JSON hold comments?
π
°οΈ No, comments arenβt allowedβuse external docs.
Q3: What is JSONP?
π
°οΈ JSONP is a workaround for cross-domain requests using script injections.
Q4: JSON vs XML β which is better?
π
°οΈ JSON is lighter, easier to read, and faster to parse.
Q5: Can JSON represent dates?
π
°οΈ Not natively. Use strings (e.g. "2025-06-25T12:00:00Z"
).
Share Now :