Skip to main content

Strings and Numbers

Learning Focus

Strings and numbers are the most frequently used JSON primitives. Getting their encoding exactly right prevents the majority of common parser errors.

Strings

A JSON string is a sequence of Unicode characters enclosed in double quotes.

{
"simple": "Hello, World!",
"withQuotes": "She said "hello"",
"withSlash": "C:\Users\alice",
"newline": "Line 1
Line 2",
"tab": "Col1 Col2",
"unicode": "\u0041\u0042\u0043 (ABC via unicode escapes)",
"empty": ""
}

Common String Patterns

Use CaseEncoding
Date"2024-01-15T08:30:00Z" (ISO 8601)
UUID"550e8400-e29b-41d4-a716-446655440000"
Binary data"SGVsbG8gV29ybGQ=" (Base64)
Large integer"9007199254740993" (send as string)

Numbers

JSON has a single number type — no distinction between int, float, long.

{
"integer": 42,
"negative": -17,
"float": 3.14159,
"scientific": 1.5e10,
"negExp": 2.8E-4
}

Invalid Formats (RFC 8259)

0xFF ✗ hexadecimal
0755 ✗ octal
007 ✗ leading zero
+42 ✗ explicit plus
.5 ✗ no leading digit
NaN ✗ not supported
Infinity✗ not supported

IEEE 754 Precision Warning

JavaScript parses JSON numbers as 64-bit doubles. Integers above 2^53 - 1 silently lose precision:

{ "id": 9007199254740993 }

→ JavaScript reads 9007199254740992. Fix: transmit large integers as strings.

Concept Map

Concept Flow

JSON Primitives
├── String → Double-quoted Unicode + Escape sequences
└── Number
├── Integer / Float / Scientific notation
└── IEEE 754 Precision Limits (> 2^53 use strings)

Common Pitfalls

PitfallConsequencePrevention
Single-quoted stringsParse errorAlways use "
Leading zerosParse errorUse 0, not 007
Large int as numberSilent precision loss in JSUse strings for IDs > 2^53
NaN / InfinityParse errorSubstitute null

What's Next