**By Dev Tools Weekly** | jsonformat.co
Common patterns, error types, and debugging tips.
JSON Syntax Rules (The Basics)
| Rule | Valid ✅ | Invalid ❌ |
|---|---|---|
| Strings use double quotes | "name" | 'name' or name |
| Keys must be quoted | {"key": 1} | {key: 1} |
| No trailing commas | [1, 2, 3] | [1, 2, 3,] |
| No comments | — | // comment or /* */ |
| No single quotes | "hello" | 'hello' |
| No undefined | null | undefined |
| Numbers: no leading zeros | 0.5 | 05 or .5 |
| No hex numbers | 255 | 0xFF |
| No Infinity/NaN | use null | Infinity, NaN |
JSON Data Types
| Type | Example | Notes |
|---|---|---|
| **String** | "hello" | UTF-8, escaped with \ |
| **Number** | 42, 3.14, -1, 2.5e10 | No hex, octal, or Infinity |
| **Boolean** | true, false | Lowercase only |
| **Null** | null | Lowercase only |
| **Array** | [1, "two", null] | Ordered, mixed types OK |
| **Object** | {"key": "value"} | Unordered key-value pairs |
Common Parse Errors & Fixes
1. Unexpected Token
SyntaxError: Unexpected token ' in JSON at position 5
Cause: Single quotes instead of double quotes.
Fix: Replace all ' with " for strings.
2. Trailing Comma
SyntaxError: Unexpected token } in JSON at position 28
Cause: {"a": 1, "b": 2,} — comma before closing brace/bracket.
Fix: Remove the last comma.
3. Unescaped Special Characters
SyntaxError: Unexpected token (newline) in JSON
Cause: String contains literal newline, tab, or backslash.
Fix: Escape them: \n, \t, \\.
4. Unquoted Keys
SyntaxError: Unexpected token k in JSON
Cause: {key: "value"} — keys must be quoted.
Fix: {"key": "value"}.
5. Missing Comma
SyntaxError: Expected ',' or '}' after property value
Cause: Missing comma between properties.
Fix: Add commas between all key-value pairs.
JSON Schema — Quick Reference
Basic Structure
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 },
"email": { "type": "string", "format": "email" }
},
"required": ["name", "email"]
}
Type Keywords
| Type | Validates | Extra Keywords |
|---|---|---|
string | Text | minLength, maxLength, pattern, format |
number | Any number | minimum, maximum, multipleOf, exclusiveMinimum |
integer | Whole numbers | Same as number |
boolean | true/false | — |
null | null only | — |
array | Arrays | items, minItems, maxItems, uniqueItems |
object | Objects | properties, required, additionalProperties |
String Formats (Built-in)
| Format | Validates | Example |
|---|---|---|
"email" | Email address | [email protected] |
"uri" | Full URI | https://example.com/path |
"date" | ISO 8601 date | 2026-02-04 |
"date-time" | ISO 8601 datetime | 2026-02-04T12:00:00Z |
"time" | ISO 8601 time | 12:00:00Z |
"ipv4" | IPv4 address | 192.168.1.1 |
"ipv6" | IPv6 address | ::1 |
"uuid" | UUID | 550e8400-e29b-41d4-a716-446655440000 |
"hostname" | Internet hostname | example.com |
"regex" | ECMA regex | ^[a-z]+$ |
Composition Keywords
// Must match ALL schemas
{ "allOf": [{ "type": "string" }, { "minLength": 1 }] }
// Must match at least ONE
{ "anyOf": [{ "type": "string" }, { "type": "number" }] }
// Must match EXACTLY one
{ "oneOf": [{ "type": "string" }, { "type": "integer" }] }
// Must NOT match
{ "not": { "type": "null" } }
// Conditional
{ "if": { "properties": { "type": { "const": "business" }}},
"then": { "required": ["company"] },
"else": { "required": ["firstName"] }
}
Common Patterns
Enum (fixed values):
{ "type": "string", "enum": ["active", "inactive", "pending"] }
Nested object:
{
"type": "object",
"properties": {
"address": {
"type": "object",
"properties": {
"street": { "type": "string" },
"city": { "type": "string" },
"zip": { "type": "string", "pattern": "^[0-9]{5}$" }
},
"required": ["street", "city"]
}
}
}
Array of objects:
{
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" }
},
"required": ["id", "name"]
},
"minItems": 1,
"uniqueItems": true
}
Debugging JSON — Pro Tips
- **Validate first, debug second.** Paste into [jsonformat.co](https://jsonformat.co) — it shows the exact line and column of errors.
- **Minified JSON unreadable?** Pretty-print it. Most errors become obvious with indentation.
- **Copy from API response?** Watch for trailing content — some APIs return multiple JSON objects (NDJSON).
- **Unicode issues?** JSON requires UTF-8. BOM (
\uFEFF) at the start will break parsers. - **Large numbers?** JavaScript loses precision above
2^53 - 1(9007199254740991). Use strings for big IDs.
jq Cheat Sheet (CLI JSON Processing)
| Command | What It Does | |
|---|---|---|
jq '.' | Pretty-print | |
jq '.name' | Extract field | |
jq '.users[0]' | First array element | |
jq '.users[] \ | .name' | All names from array |
jq 'length' | Count elements | |
jq 'keys' | List object keys | |
jq 'select(.age > 21)' | Filter | |
jq '{name, email}' | Pick fields | |
jq -r '.url' | Raw output (no quotes) | |
jq -c '.' | Compact (minify) |
🛠 Format & validate JSON instantly: jsonformat.co
📧 More cheat sheets: Dev Tools Weekly Newsletter