JS का JSON
In this page:
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?
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()
// 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()
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
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
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
- JSON text में single quotes या trailing commas इस्तेमाल करना, जैसे
{a: 1}, जोJSON.parseकोSyntaxErrorthrow करवाता है। - यह उम्मीद करना कि
JSON.stringifyfunctions,undefinedयाDateobjects को रखेगा, जिन्हें drop कर दिया जाता है या strings में बदल दिया जाता है। - किसी पहले से parsed object पर
JSON.parseकॉल करना, इसलिए यह"[object Object]"बन जाता है और fail होता है।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS JSON
- JS Regular Expressions
- JS Fetch API
- JS LocalStorage and SessionStorage
- JS Cookies
- JS setTimeout and setInterval
- JS Event Loop
- JS Web Workers
- JS Service Workers
- JS AJAX
- JS AJAX Intro
- JS AJAX XMLHttp
- JS AJAX Request
- JS AJAX Response
- JS AJAX XML
- JS AJAX PHP
- JS AJAX Database
- JS JSONP
- JS RegExp Flags
- JS RegExp Classes
- JS RegExp Metachars
- JS RegExp Assertions
- JS RegExp Groups
- JS RegExp Quantifiers
- JS JSON HTML
- JS JSON vs XML