Theory
counter કહે છે 3501
એક teammate Garba Night record ને hand-write કરે છે, booking code cancellation refund માટે seats + 1 ને runs કરે છે, અને display proudly બતાવે છે 3501 seats.
તેનું JSON: { "seats": "350" }.
એક pair of quotation marks એ number ને text માં turn કર્યું, અને JavaScript ના + politely concatenate કર્યું. કંઈ crash ન થયું; data simply wrong TYPE નું હતું.
JSON objects easy છે; JSON objects right string/number discipline સાથે આ lesson છે.
Theory
object, ઔપચારિક રીતે
JSON object એ unordered set છે key-value pairs નું braces ની અંદર:
{ "name": "Garba Night", "seats": 350 }
Rules with teeth:
- દરેક key એ double-quoted string છે:
{seats: 350}fine JS literal છે અને invalid JSON છે - pairs commas સાથે separate થાય છે; last પછી કોઈ trailing comma નહીં
- values પોતે objects હોઈ શકે છે: nesting structure build કરે છે
- એક object ની અંદર, keys unique હોવી જોઈએ
Theory
Strings અને numbers, precisely
Strings: double quotes, હંમેશા: "Garba Night". Single quotes invalid છે. Special characters backslash સાથે escape થાય છે: "Main \"A\" Ground", new line માટે \n, literal backslash માટે \\.
Numbers: bare લખાય છે, કોઈ quotes નહીં: 350, 49.5, -10, અને exponent form 1.5e3 (જે 1500 છે). બે illegalities: leading zeros (042 rejected થાય છે) અને special values NaN/Infinity, જે JSON જાણતું નથી.
boundary rule: quotes તેને string બનાવે છે, bare તેને number બનાવે છે, અને 2 parsing પછી utterly differently behave કરે છે.
Practical
Right types vs wrong types (run it)
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 nesting ને walk કરે છે
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 છે?
- { seats: 350 }
- { "seats": 350 }
- { "seats": 042 }
- { "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 કરે છે: સૌથી common conversion error જ્યારે developers JSON ને hand-write કરે છે. Option C leading-zero ban પર trips થાય છે: 042 legal JSON number નથી (42 લખો, અથવા "042" જો તે genuinely code હોય જેમ કે roll number). Option D નું single quotes strings rule ને fail કરે છે. wrong options માં pattern નો note: દરેક SOME nearby language માં legal છે, જે exactly છે કેવી રીતે આ mistakes typed થાય છે.
Think first
String or number: 4 fields પર rule
ચાર fest fields: seats left, 10-digit helpline number, entry fee, PIN code જેમ કે 395007. દરેક માટે: bare number અથવા quoted string JSON માં? Decide કરો reason સાથે tap કરતા પહેલાં.
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 કરશે? Yes = bare number; no, તે identifier છે જે merely numeric લાગે છે = string. આ wrong મેળવવું એ રીતે 3501 bug અને તેના cousins born થાય છે.
Watch out
ત્રણ object-writing slips
quoted number: "seats": "350" દરેક parser ને pass કરે છે અને first addition પર fail થાય છે: worst kind નો bug, silent જ્યાં સુધી arithmetic ન થાય.
trailing comma: { "a": 1, } ને JSON.parse reject કરે છે; editors જે તેને JS માં tolerate કરે છે તે તમને habit શીખવે છે જેને JSON punish કરે છે.
Escaping forgotten: venue નું નામ Main "A" Ground એ તેના inner quotes ને escape કરવા પડે, અથવા string early end થાય છે અને parse mid-key પર dies થાય છે.
Theory
Nesting એ તમારો data model છે
notice કરો કે nested venue object એ શું કર્યું: તે hall અને capacity ને એક name ની નીચે group કર્યા, exactly જેમ class fields ને group કરે છે (BCA403 નું Book, BCA404 નું Item). JSON ને design કરો જેમ તમે objects ને design કરો છો: things જે belong કરે છે together તેને nest together, અને chained dots (ev.venue.hall) sentence જેમ read થાય છે. એક shape હજુ missing છે real data માટે: events ની LISTS, prices ની, seat rows ની: JSON arrays બધા 5 syllabus forms માં, next.
Summary
Key takeaways
- Object = { "key": value, ... }: double-quoted keys, comma-separated, no 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; chained dots levels ને walk કરે છે JSON.parse પછી.
- Memory hook: words માટે quotes, maths માટે bare.