JSON objects (with string and numbers)

एक JSON object double-quoted keys है जो values पर map होती हैं: strings हमेशा double quotes में, numbers bare (कोई quotes नहीं, कोई leading zeros नहीं), और एक number को quote करना चुपचाप इसे unusable text बना देता है।

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Counter 3501 कहता है

एक teammate hand-writes Garba Night record, booking code एक cancellation refund के लिए seats + 1 run करता है, और display proudly 3501 seats दिखाता है।

उसका JSON: { "seats": "350" }।

Quotation marks की एक pair ने एक number को text बना दिया, और JavaScript का + politely concatenate कर गया। कुछ crash नहीं हुआ; data बस ग़लत TYPE का था।

JSON objects आसान हैं; सही string/number discipline वाले JSON objects यह lesson हैं।

Theory

Object, Formally

एक JSON object braces के अंदर key-value pairs का एक unordered set है:

{ "name": "Garba Night", "seats": 350 }

Rules जिनके teeth हैं:

  • हर key एक double-quoted string है: {seats: 350} एक fine JS literal है और invalid JSON
  • pairs commas से separate होते हैं; आख़िरी के बाद कोई trailing comma नहीं
  • values खुद objects हो सकते हैं: nesting structure बनाता है
  • एक object के अंदर, keys unique होनी चाहिए

Theory

Strings और Numbers, Precisely

Strings: double quotes, हमेशा: "Garba Night"। Single quotes invalid हैं। Special characters एक backslash से escape होते हैं: "Main \"A\" Ground", नई line के लिए \n, literal backslash के लिए \\।

Numbers: bare लिखे जाते हैं, कोई quotes नहीं: 350, 49.5, -10, और exponent form 1.5e3 (जो 1500 है)। दो illegalities: leading zeros (042 reject होता है) और special values NaN/Infinity, जिन्हें JSON नहीं जानता।

Boundary rule: quotes इसे string बनाते हैं, bare इसे number बनाता है, और parsing के बाद दोनों बिल्कुल अलग तरह behave करते हैं।

Practical

सही Types बनाम ग़लत Types (इसे Run कीजिए)

const good = JSON.parse('{ "name": "Garba Night", "seats": 350 }');
const bad  = JSON.parse('{ "name": "Garba Night", "seats": "350" }');

console.log(good.seats + 1);   // 351: number maths
console.log(bad.seats + 1);    // "3501": string concatenation!

// Nested object: structure inside structure
const text = '{ "name": "Garba Night", "venue": { "hall": "Main Ground", "capacity": 350 } }';
const ev = JSON.parse(text);
console.log(ev.venue.hall);       // chained dots walk the nesting
console.log(ev.venue.capacity - 50);  // 300

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

इनमें से कौन सी line valid JSON है?

  1. { seats: 350 }
  2. { "seats": 350 }
  3. { "seats": 042 }
  4. { "seats": '350' }
Show the answer

{ "seats": 350 }

Double-quoted key, bare number value: option B हर rule check करता है। Option A का unquoted key वही JS-literal habit है जिसे JSON refuse करता है: developers hand-write JSON करते समय सबसे common conversion error। Option C leading-zero ban पर trip करता है: 042 एक legal JSON number नहीं है (42 लिखिए, या अगर यह genuinely एक roll number जैसा code है तो "042")। Option D के single quotes strings rule fail करते हैं। ग़लत options में pattern नोटिस कीजिए: हर एक किसी NEARBY language में legal है, यही exactly वजह है ये mistakes type होती हैं।

Think first

String या Number: 4 Fields पर Rule कीजिए

चार fest fields: seats left, एक 10-digit helpline number, entry fee, एक PIN code जैसे 395007। हर एक के लिए: JSON में bare number या quoted string? Tap करने से पहले एक reason के साथ decide कीजिए।

Show the answer

Seats: number (आप इसे count और subtract करते हैं)। Fee: number (arithmetic: totals, discounts)। Helpline: string: आप कभी phone numbers add नहीं करते, leading zeros को survive करना ज़रूरी है, और एक + prefix appear हो सकता है: इसे quote कीजिए। PIN code: string same reasons से (एक leading-zero PIN number की तरह corrupted हो जाता)। Working test: क्या code इसके साथ MATHS करेगा? हाँ = bare number; नहीं, यह सिर्फ़ numeric दिखने वाला एक identifier है = string। इसे ग़लत करना है जिससे 3501 bug और इसके cousins born होते हैं।

Watch out

तीन Object-Writing Slips

Quoted Number: "seats": "350" हर parser pass कर जाता है और पहले addition पर fail होता है: worst kind का bug, arithmetic तक silent।

Trailing Comma: { "a": 1, } JSON.parse से reject होता है; JS में इसे tolerate करने वाले editors आपको वह habit सिखाते हैं जिसे JSON punish करता है।

Escaping भूल जाना: Main "A" Ground नाम के एक venue को अपने inner quotes escape करने ज़रूरी हैं, वरना string जल्दी खत्म हो जाती है और parse mid-key मर जाता है।

Theory

Nesting आपका Data Model है

नोटिस कीजिए nested venue object ने क्या किया: इसने hall और capacity को एक नाम के नीचे group किया, exactly जैसे एक class fields group करती है (BCA403 की Book, BCA404 का Item)। JSON को उसी तरह design कीजिए जैसे आप objects design करते हैं: जो चीज़ें साथ belong करती हैं वे साथ nest होती हैं, और chained dots (ev.venue.hall) एक sentence जैसे पढ़ते हैं। असली data के लिए एक shape अभी भी missing है: events की, prices की, seat rows की LISTS: JSON arrays अपने सारे 5 syllabus forms में, अगला।

Summary

Key takeaways

  • Object = { "key": value, ... }: double-quoted keys, comma-separated, कोई trailing comma नहीं।
  • Strings: सिर्फ़ double quotes; inner quotes और specials को backslash से escape कीजिए।
  • Numbers: bare: integers, decimals, negatives, exponents; कोई quotes नहीं, कोई leading zeros नहीं, कोई NaN/Infinity नहीं।
  • "350" text है: + concatenate करता है (3501); 350 एक number है: + add करता है (351)।
  • जो identifiers numeric दिखते हैं (phone, PIN) strings हैं; सिर्फ़ maths-bound values numbers हैं।
  • Objects nest होते हैं; JSON.parse के बाद chained dots levels walk करते हैं।
  • Memory hook: words के लिए quotes, maths के लिए bare।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from JSON (JavaScript Object Notation)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

JSON objects (with string and numbers) · Web Designing-2 (option A) · Gri-Learn