Formula & Mathematical Methodology
Strict JSON (RFC-8259) rejects single quotes, unquoted property names, `undefined` values, `NaN`/`Infinity`, and trailing commas. Validating and canonicalizing JSON (with alphabetical key sorting) guarantees deterministic cache hashes in Cloudflare Workers and Durable Objects.
Minification Savings % = [(Raw UTF-8 Bytes − Minified UTF-8 Bytes) / Raw UTF-8 Bytes] × 100
- RFC-8259 — IETF standard requiring double-quoted keys/strings, no trailing commas, and no comments.
- Key Count — Recursive total of all object property keys across every nested level.
- Max Depth — Deepest nested object/array hierarchy level in the payload.
Step-by-Step Worked Example
Scenario: A developer validates a 121-byte JSON configuration object containing 5 top-level keys and an embedded 4-item array.
- Parser verifies strict RFC-8259 compliance: 5 object keys, maximum nesting depth = 2 levels.
- Pretty-printing with 2-space indentation formats the structure clearly for code review.
- Enabling alphabetical key sorting reorders keys (`edge`, `latencyMs`, `project`, `tools`, `version`) for deterministic hashing.
Key Takeaway: Sorting keys before hashing JSON payloads ensures `{a:1, b:2}` and `{b:2, a:1}` hit the exact same SQLite cache key.
Practical Operator & Planning Tips
- Always enable alphabetical key sorting when generating deterministic cache keys for LLM prompts or webhook signature verification.
- Keep JSON API payloads under 4 levels of nesting—deeply nested trees complicate client null-checking and GraphQL/SQL serialization.
- When embedding JSON-LD inside HTML `<script type="application/ld+json">` tags, always output minified 0-space JSON to shave 15%–25% off HTML document weight.
- Never store raw floating-point currency amounts (like `19.990000000002`) in billing JSON; transmit integer minor units (e.g., `1999` cents) or fixed decimal strings.
Frequently Asked Questions
Why does JSON.parse fail on trailing commas that work fine in JavaScript?
The RFC-8259 JSON specification intentionally kept grammar minimal for cross-language parsers and strictly forbids trailing commas after the final element in an array or object.
Can valid JSON contain comments?
Standard `.json` files do not allow `//` or `/* */` comments. Configuration files that allow comments (such as `wrangler.jsonc` or `tsconfig.json`) use the JSON with Comments (JSONC) superset.
Does sorting JSON keys change the meaning of the data?
In RFC-8259, an object is an unordered collection of name/value pairs, so sorting object keys preserves semantics while leaving array element order untouched.