JSON Parsing Guide: From Text to Typed Data Safely
How JSON parsing works across languages, common pitfalls with numbers and dates, and safer patterns for production code.
Parsing is deserialization
JSON parsing turns a string of text into in-memory values: maps/objects, lists/arrays, strings, numbers, booleans, and null. Serialization goes the other direction. Most bugs appear at these boundaries—when assumptions about shape or types are wrong.
Language snapshots
JavaScript
const data = JSON.parse(text);
const text2 = JSON.stringify(data);
JSON.parse throws SyntaxError on invalid input—always wrap untrusted input in try/catch.
Python
import json
data = json.loads(text)
text2 = json.dumps(data)
Go roughly uses json.Unmarshal into structs with tags.
Regardless of language, prefer parsing into typed structures when the contract is known.
Trust boundaries
Never parse untrusted JSON without:
- Size limits
- Timeouts for huge inputs
- Schema or structural validation for public endpoints
- Careful handling of deeply nested documents (stack/depth limits)
Parser differentials and giant nested arrays are real denial-of-service vectors.
Numbers, precision, and integers
JSON numbers are typically IEEE-754 doubles in JavaScript. That means integers above Number.MAX_SAFE_INTEGER (2^53−1) can lose precision. If you need large IDs, keep them as strings:
{ "id": "9007199254740993" }
Other languages may offer big integers or decimal types—document your choice in the API.
Dates are not a native type
JSON has no date type. Common patterns:
- ISO-8601 strings:
"2026-06-16T12:00:00Z" - Unix epoch numbers (seconds or milliseconds—specify which!)
Parse deliberately. Do not invent ambiguous formats like 06/16/2026 in APIs.
null vs missing keys
{ "nickname": null }
is not the same as {} without nickname. Decide whether your API uses null to clear a field, and teach clients the difference. Parsers preserve null; they cannot invent missing keys.
Revivers and transformers
JavaScript’s JSON.parse(text, reviver) can transform values during parse (for example, turning ISO strings into Date). Use sparingly—revivers that recurse unexpectedly can surprise maintainers. Explicit mapping layers are easier to test.
Debugging parse failures
When JSON.parse fails, log a truncated snippet and the error position if available—not the entire multi-megabyte body. Reproduce with a validator, fix the payload source, and add a regression fixture. JSON Editor is useful for isolating whether the problem is syntax or your mapping code.
Safer production pattern
- Read raw bytes/string with size caps.
- Parse JSON in try/catch (or equivalent).
- Validate with schema or decoder.
- Map into domain types.
- Reject unknown critical fields if your policy says so.
Parsing is the doorway to your domain model. Keep the doorway narrow, well-lit, and tested—and your application logic can assume shape instead of defensively checking every key forever.
Related articles
Guides · 3 min read
How to Fix Invalid JSON: A Field Guide to Common Errors
Diagnose and repair invalid JSON—trailing commas, quotes, brackets, and encoding issues—with clear examples and a repair workflow.
Guides · 3 min read
JSON Schema Guide: Contracts That Keep APIs Honest
An introduction to JSON Schema for validating structure, types, and required fields with practical examples and workflow tips.
Guides · 3 min read
JSONPath Explained: Query JSON Without Writing Loops
Learn JSONPath syntax, common selectors, practical examples, and how path queries speed up exploration of nested documents.