JSON (JavaScript Object Notation) is the universal language of data exchange on the web. Whether you're building APIs, storing configuration files, or communicating between services, understanding JSON is essential for any developer. This comprehensive guide takes you from the basics to advanced concepts.
JSON क्या है?
JSON is a lightweight, text-based data interchange format. Despite "JavaScript" in the name, JSON is language-independent and supported by virtually every programming language. It was derived from JavaScript object syntax but has become the de facto standard for data exchange.
💡 Fun Fact: JSON was popularized by Douglas Crockford in the early 2000s. It's now defined by two standards: RFC 8259 and ECMA-404. The format is so simple that the entire specification fits on a business card.
JSON सिंटैक्स: मूल बातें
JSON is built on two structures:
- Objects: Collections of key-value pairs enclosed in curly braces
{} - Arrays: Ordered lists of values enclosed in square brackets
[]
एक साधारण JSON ऑब्जेक्ट
JSON डेटा प्रकार
JSON supports six data types:
| Type | Example | Description |
|---|---|---|
| String | "Hello World" |
Text enclosed in double quotes |
| Number | 42, 3.14, -17 |
Integer or floating-point |
| Boolean | true, false |
Logical values (lowercase) |
| Null | null |
Empty or absent value |
| Object | {"key": "value"} |
Unordered key-value pairs |
| Array | [1, 2, 3] |
Ordered list of values |
नेस्टेड संरचनाएँ
JSON's power comes from nesting objects and arrays:
"user": {
"id": 12345,
"profile": {
"firstName": "Jane",
"lastName": "Smith"
},
"tags": ["developer", "designer"]
}
}
सामान्य JSON गलतियाँ
⚠️ JSON is Strict: Unlike JavaScript objects, JSON requires exact syntax. A single error will cause parsing to fail completely.
1. अनुगामी अल्पविराम
{ "name": "John", "age": 30, }
// ✅ Valid
{ "name": "John", "age": 30 }
2. एकल उद्धरण
{ 'name': 'John' }
// ✅ Valid
{ "name": "John" }
3. अउद्धृत कुंजियाँ
{ name: "John" }
// ✅ Valid
{ "name": "John" }
4. टिप्पणियाँ
{
"name": "John" // This is a name
}
// ✅ JSON simply doesn't allow comments
जावास्क्रिप्ट में JSON के साथ कार्य करना
पार्सिंग JSON (स्ट्रिंग → ऑब्जेक्ट)
const user = JSON.parse(jsonString);
console.log(user.name); // "John"
console.log(user.age); // 30
स्ट्रिंगिफ़ाइंग (ऑब्जेक्ट → स्ट्रिंग)
const jsonString = JSON.stringify(user);
console.log(jsonString);
// '{"name":"John","age":30}'
सुंदर मुद्रण
// Third parameter = indentation spaces
const pretty = JSON.stringify(user, null, 2);
console.log(pretty);
// {
// "name": "John",
// "age": 30
// }
✅ प्रो टिप: सुरक्षित पार्सिंग
Always wrap JSON.parse() in try-catch to handle invalid JSON gracefully instead of crashing your application.
पायथन में JSON के साथ कार्य करना
# Parsing JSON
json_string = '{"name": "John", "age": 30}'
user = json.loads(json_string)
print(user["name"]) # John
# Creating JSON
user_dict = {"name": "Jane", "age": 25}
json_output = json.dumps(user_dict, indent=2)
print(json_output)
JSON बनाम XML: प्रत्येक का उपयोग कब करें
| Aspect | JSON | XML |
|---|---|---|
| Readability | More readable for humans | More verbose |
| File Size | Smaller | Larger (more markup) |
| Parsing Speed | Faster | Slower |
| Data Types | Native types (numbers, booleans) | Everything is a string |
| Comments | Not supported | Supported |
| Namespaces | Not supported | Supported |
| Best For | APIs, web apps, config files | Documents, complex schemas |
वास्तविक दुनिया JSON उदाहरण
एपीआई प्रतिक्रिया
"status": "success",
"data": {
"users": [
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
],
"total": 2
}
}
कॉन्फ़िगरेशन फ़ाइल (पैकेज.json)
"name": "my-project",
"version": "1.0.0",
"dependencies": {
"express": "^4.18.0",
"lodash": "^4.17.21"
}
}
सर्वोत्तम प्रथाएं
- Use descriptive keys:
"firstName"is better than"fn" - Be consistent with naming: Choose camelCase or snake_case and stick with it
- Validate before parsing: Always validate JSON from external sources
- Handle null values explicitly: Don't omit keys—use
nullfor empty values - Use arrays for lists: Even if there's currently only one item
- Keep nesting reasonable: Deeply nested JSON is hard to work with
🔧 JSON को प्रारूपित करने या सत्यापित करने की आवश्यकता है?
Use our free JSON Formatter tool to beautify, validate, and minify JSON instantly.
JSON फ़ॉर्मेटर खोलें →निष्कर्ष
JSON's simplicity is its strength. With just objects, arrays, and six data types, you can represent virtually any data structure. Master the basics covered here, avoid the common pitfalls, and you'll be working with JSON like a pro.
Remember: when in doubt, validate your JSON with a tool before debugging your code. Nine times out of ten, the issue is a syntax error—usually a missing comma or quote.