← Back to JavaScript Course | Chapter 9: Async & Web APIs | Lesson 1 of 26

JS का JSON

JSON data को text के रूप में लिखने का एक सरल तरीका है ताकि programs उसे साझा कर सकें, जैसे एक standard packing list। JavaScript objects को JSON text में बदल सकता है और उन्हें वापस पढ़ सकता है।
Syntax
javascript
const text = JSON.stringify(object);
const object = JSON.parse(text);

JSON क्या है?

JSON data store और exchange करने के लिए इस्तेमाल होने वाला एक text format है। इसके सरल, language-independent syntax ने JSON को web APIs, configuration files, और server व browser के बीच भेजे जाने वाले data के लिए standard format बना दिया।

उदाहरण: What Is JSON?

javascript
const data = { name: "Sam", age: 30 };
const jsonString = JSON.stringify(data);
console.log(jsonString); // used to exchange data with APIs

JSON.parse()

JSON.parse() किसी JSON string को एक JavaScript value में बदल देता है। अगर string valid JSON न हो, तो JSON.parse() एक SyntaxError throw करता है, इसलिए untrusted input को parse करना आमतौर पर एक try/catch में लपेटा जाना चाहिए।

उदाहरण: JSON.parse()

javascript
// Declare the constant `jsonString`, set to '{"name": "Sam", "age": 30}'
// Declare the constant `jsonString`, set to '{"name": "Sam", "age": 30}'
const jsonString = '{"name": "Sam", "age": 30}';
// Declare the constant `obj`, set to `JSON.parse(jsonString)`
// Declare the constant `obj`, set to `JSON.parse(jsonString)`
const obj = JSON.parse(jsonString);
// Print `obj.name` to the console
// Print `obj.name` to the console
console.log(obj.name);

JSON.stringify()

JSON.stringify() किसी JavaScript value को एक JSON string में बदल देता है।

Functions, undefined values, और symbols को JSON.stringify() चुपचाप छोड़ देता है, क्योंकि JSON के पास उन्हें represent करने का कोई तरीका नहीं है — किसी round-trip को lossless मानने से पहले यह जानना ज़रूरी है।

उदाहरण: JSON.stringify()

javascript
const data = { name: "Sam", greet: function () {}, id: undefined };
console.log(JSON.stringify(data)); // functions/undefined dropped

JSON Data Types

JSON strings, numbers, booleans, null, objects, और arrays को support करता है।

चूंकि JSON में dates या functions जैसी कोई चीज़ नहीं है, इन्हें stringify करने से पहले साधारण values में बदलना ज़रूरी है (जैसे एक ISO date string), और parse करने के बाद manually वापस बदलना ज़रूरी है।

उदाहरण: JSON Data Types

javascript
console.log(JSON.stringify({ a: 1, b: "text", c: true, d: null, e: [1, 2] }));

Practical JSON

JSON आमतौर पर APIs, configuration files, और saved application data के साथ इस्तेमाल होता है।

एक typical fetch() call response.json() से एक JSON response body को parse करता है, और एक typical POST request किसी JavaScript object को request body में भेजने से पहले stringify करता है।

उदाहरण: Practical JSON

javascript
const payload = JSON.stringify({ name: "Sam" });
console.log(payload); // typical POST body
const response = JSON.parse(payload);
console.log(response.name); // typical parsed API response
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. JSON text में single quotes या trailing commas इस्तेमाल करना, जैसे {a: 1}, जो JSON.parse को SyntaxError throw करवाता है।
  2. यह उम्मीद करना कि JSON.stringify functions, undefined या Date objects को रखेगा, जिन्हें drop कर दिया जाता है या strings में बदल दिया जाता है।
  3. किसी पहले से parsed object पर JSON.parse कॉल करना, इसलिए यह "[object Object]" बन जाता है और fail होता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.