JSON objects (with string and numbers)

A JSON object is double-quoted keys mapping to values: strings always in double quotes, numbers bare (no quotes, no leading zeros), and quoting a number quietly turns it into unusable text.

10 min read · 9 cards · 2 checks

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


Theory

The counter says 3501

A teammate hand-writes the Garba Night record, the booking code runs seats + 1 for a cancellation refund, and the display proudly shows 3501 seats.

His JSON: { "seats": "350" }.

One pair of quotation marks turned a number into text, and JavaScript's + politely concatenated. Nothing crashed; the data was simply the wrong TYPE.

JSON objects are easy; JSON objects with the right string/number discipline are this lesson.

Theory

The object, formally

A JSON object is an unordered set of key-value pairs inside braces:

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

Rules with teeth:

  • every key is a double-quoted string: {seats: 350} is a fine JS literal and invalid JSON
  • pairs separate with commas; no trailing comma after the last
  • values may themselves be objects: nesting builds structure
  • within one object, keys should be unique

Theory

Strings and numbers, precisely

Strings: double quotes, always: "Garba Night". Single quotes are invalid. Special characters escape with a backslash: "Main \"A\" Ground", \n for a new line, \\ for a literal backslash.

Numbers: written bare, no quotes: 350, 49.5, -10, and exponent form 1.5e3 (which is 1500). Two illegalities: leading zeros (042 is rejected) and the special values NaN/Infinity, which JSON does not know.

The boundary rule: quotes make it a string, bare makes it a number, and the 2 behave utterly differently after parsing.

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 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

Which line is 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 checks every rule. Option A's unquoted key is the JS-literal habit JSON refuses: the single most common conversion error when developers hand-write JSON. Option C trips the leading-zero ban: 042 is not a legal JSON number (write 42, or "042" if it is genuinely a code like a roll number). Option D's single quotes fail the strings rule. Note the pattern in the wrong options: each is legal in SOME nearby language, which is exactly how these mistakes get typed.

Think first

String or number: rule on 4 fields

Four fest fields: seats left, a 10-digit helpline number, the entry fee, a PIN code like 395007. For each: bare number or quoted string in JSON? Decide with a reason before tapping.

Show the answer

Seats: number (you count and subtract it). Fee: number (arithmetic: totals, discounts). Helpline: string: you never add phone numbers, leading zeros must survive, and a + prefix may appear: quote it. PIN code: string for the same reasons (a leading-zero PIN would be corrupted as a number). The working test: will code do MATHS with it? Yes = bare number; no, it is an identifier that merely looks numeric = string. Getting this wrong is how the 3501 bug and its cousins are born.

Watch out

Three object-writing slips

The quoted number: "seats": "350" passes every parser and fails at the first addition: the worst kind of bug, silent until arithmetic.

The trailing comma: { "a": 1, } is rejected by JSON.parse; editors that tolerate it in JS teach you the habit JSON punishes.

Escaping forgotten: a venue named Main "A" Ground must escape its inner quotes, or the string ends early and the parse dies mid-key.

Theory

Nesting is your data model

Notice what the nested venue object did: it grouped hall and capacity under one name, exactly like a class groups fields (BCA403's Book, BCA404's Item). Design JSON the way you design objects: things that belong together nest together, and chained dots (ev.venue.hall) read like a sentence. One shape is still missing for real data: LISTS of events, of prices, of seat rows: JSON arrays in all 5 syllabus forms, next.

Summary

Key takeaways

  • Object = { "key": value, ... }: double-quoted keys, comma-separated, no trailing comma.
  • Strings: double quotes only; escape inner quotes and specials with backslash.
  • Numbers: bare: integers, decimals, negatives, exponents; no quotes, no leading zeros, no NaN/Infinity.
  • "350" is text: + concatenates (3501); 350 is a number: + adds (351).
  • Identifiers that look numeric (phone, PIN) are strings; only maths-bound values are numbers.
  • Objects nest; chained dots walk the levels after JSON.parse.
  • Memory hook: quotes for words, bare for maths.

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