JSON pitfalls: large numbers, dates, duplicate keys and other surprises
JSON's grammar fits on a business card, and that simplicity is why it won. The catch is that the specification says very little about what the values mean. The gaps are filled in differently by each language's parser, and that is where the surprises come from.
What is and is not valid JSON
JSON is stricter than the JavaScript object syntax it resembles. These are all errors:
{ name: "Ada" } // keys must be double-quoted strings
{ 'name': 'Ada' } // single quotes are not allowed
{ "a": 1, } // no trailing commas
{ "a": 1 /* note */ } // no comments
{ "n": NaN, "m": .5 } // no NaN or Infinity; a digit is required before the point
{ "t": undefined } // undefined does not exist
The only values are objects, arrays, strings, numbers, true, false and null. Files that allow comments or trailing commas, such as many editor and compiler configuration files, are JSONC or JSON5: related formats that a strict JSON parser will reject. A JSON validator will point at the exact character where a document stops being valid.
Numbers lose precision without telling you
The specification puts no limit on the size or precision of a number. Parsers do. JavaScript, and many libraries in other languages by default, read every JSON number into a 64-bit floating-point value, which can represent integers exactly only up to 9,007,199,254,740,991 (253 − 1).
JSON.parse('{"id": 9007199254740993}').id
// 9007199254740992 ← a different number, and no error
This bites with database IDs, snowflake-style IDs and anything else that is a 64-bit integer. The value goes out of the server correct and arrives in the browser wrong. Send large integers as strings. An identifier is not something you do arithmetic on, so nothing is lost.
The same floating-point representation is why 0.1 + 0.2 is not exactly 0.3. For money, send an integer count of the smallest unit (cents, paise) or a decimal string, and parse it into a decimal type. Never let a price pass through a binary float.
There is no date type
JSON cannot represent a date, so every API invents a convention. The ones you will meet:
"2026-03-14T09:30:00Z", an ISO 8601 string. Readable, sortable as text, and unambiguous when it includesZor an offset. This is the one to choose.1773480600, a Unix timestamp. Compact, but the reader has to know whether it is seconds or milliseconds."14/03/2026", a local format. Ambiguous between day-first and month-first; avoid it.
A parser returns a date as a plain string. Converting it to a date object is your code's job, and doing that consistently for every field is easy to forget. Decide the format once, document it, and include the offset.
Duplicate keys are undefined behaviour
{ "role": "user", "role": "admin" }
The specification says keys "should" be unique and stops there. Most parsers keep the last value. Some keep the first, and some raise an error. That disagreement has caused real security bugs: a gateway validates the document using one parser and sees user, then forwards the raw text to a service whose parser sees admin. If you validate JSON in one place and consume it in another, either reject duplicate keys outright or re-serialise what you validated instead of forwarding the original bytes.
Key order means nothing; null and missing are different
An object is an unordered collection. Most parsers happen to preserve the order keys arrived in, but nothing guarantees it, and some sort or reorder them. If order matters, use an array.
These three documents are different, and an API should say which it uses for "no value":
{ "nickname": null } // present, explicitly empty
{ "nickname": "" } // present, an empty string
{ } // absent
The distinction matters most for updates. In a partial update, a missing field usually means "leave it alone" and null means "clear it". Languages that have no separate notion of "absent" need care to keep the two apart.
Text encoding
JSON exchanged between systems must be UTF-8. Inside strings, a few characters must be escaped: the double quote, the backslash and control characters such as a raw newline or tab. A literal line break inside a string is invalid; it has to be written as \n.
Characters outside the basic range, including most emoji, are written in \u form as a pair of escapes, not one. Truncating a string by code units can cut such a pair in half and produce text that some parsers reject. Leave escaping to a serialiser; do not build JSON by joining strings together.
Large documents
A normal parser reads the entire document into memory before giving you anything, and the in-memory form is typically several times larger than the text. A 500 MB export can exhaust a process that has gigabytes available. Two approaches help:
- Newline-delimited JSON (NDJSON, JSON Lines): one complete JSON value per line. It can be produced, read and processed one record at a time, and appended to like a log.
- A streaming parser, which emits events as it reads rather than building the whole tree.
For tabular data, also ask whether JSON is the right container. Repeating every column name in every row is wasteful; CSV or a columnar format is often a better fit.
Safety
- Parse JSON with a JSON parser. Never use
evalor anything that executes the text. - Limit the size and nesting depth you accept from outside. A few kilobytes of deeply nested brackets can exhaust the stack of a recursive parser.
- Validate the shape after parsing. Syntactically valid JSON can still have the wrong types, and code that assumes an array will misbehave when sent an object. A schema validator makes the expectations explicit.
A short design checklist for a JSON API
- IDs and any integer that might exceed 253 are strings.
- Money is an integer in minor units or a decimal string, with the currency alongside.
- Timestamps are ISO 8601 with an offset.
- The meaning of
nullversus an absent field is written down. - Lists are arrays, even when order "probably" does not matter.
- Unknown fields are ignored by readers, so that fields can be added later without breaking them.