Skip to content

Guide

JSON gotchas that pass validation

A JSON document can pass every validator and still change on its way through your system. These are the gaps in the standard and the quirks of real parsers that cause the bugs, with tested JavaScript and Python examples.

By Mark Stoumann ·

The JSON formatter catches syntax errors: trailing commas, single quotes, comments. This guide is about the other kind of problem, where the JSON is perfectly valid and the data still comes out different from what went in.

1. Big integers silently change

JSON numbers have no size limit in the grammar, but almost every parser turns them into 64-bit floating point numbers, which hold integers exactly only up to 253 − 1. RFC 8259 says as much: only integers in that range are reliably interoperable. Beyond it, JavaScript rounds without any warning:

JSON.parse('{"id": 9007199254740993}').id
// 9007199254740992

This hits 64-bit IDs from databases and distributed ID generators, which are routinely larger than 253. The record still loads; it just loads the wrong record, or none. The fix is to send such IDs as strings: {"id": "9007199254740993"}. If you can’t change the producer, modern JavaScript can read the original digits through the reviver’s third argument, and write them back with JSON.rawJSON(), both available in current browsers since 2025:

const data = JSON.parse(text, (key, value, context) =>
  key === 'id' ? BigInt(context.source) : value)

JSON.stringify({ id: JSON.rawJSON(data.id.toString()) })
// '{"id":12345678901234567890}'

Note the second line: JSON.stringify throws “Do not know how to serialize a BigInt” on a plain BigInt, so you have to decide how it is written out.

Decimals have the same root cause. 0.1 + 0.2 is 0.30000000000000004 in every language that uses binary floating point, and JSON copies it faithfully. For money, send integer minor units (1999 cents) or a decimal string ("19.99"), and do the arithmetic in a decimal type.

2. Duplicate keys mean different things to different parsers

Is {"qty": 1, "qty": -1} valid JSON? The grammar allows it. RFC 8259 only says names should be unique, and that software receiving duplicates behaves unpredictably. In practice:

  • JavaScript’s JSON.parse keeps the last value.
  • Python’s json module keeps the last value, as its documentation states.
  • Others keep the first, such as the Go library buger/jsonparser.

That is harmless inside one program and dangerous between two. In 2021, Jake Miller of Bishop Fox surveyed 49 JSON parsers and described a hypothetical shop where the cart service, using Python’s parser, validated qty as 1 while the payment service, using a Go library that keeps the first key, charged for −1. Same bytes, two meanings. If a request is checked by one component and acted on by another, make the first one reject duplicates. In Python:

def no_duplicates(pairs):
    keys = [k for k, _ in pairs]
    if len(keys) != len(set(keys)):
        raise ValueError('duplicate key in JSON object')
    return dict(pairs)

json.loads(text, object_pairs_hook=no_duplicates)

3. What JSON.stringify quietly drops

JavaScript values don’t map one-to-one onto JSON. JSON.stringify resolves the mismatch by changing or dropping values instead of failing:

ValueIn an objectIn an array
undefined, functions, symbolsProperty left outnull
NaN, Infinitynullnull
-000
DateISO string, stays a string when parsedSame
Map, Set, RegExp{}{}
Uint8Array{"0":1,"1":2}Same
BigIntThrows a TypeErrorThrows a TypeError

The most common bug from this table is the Map: a cache or lookup table that serializes to {} and arrives empty. Convert first with Object.fromEntries(map) or [...map]. And because a Date comes back as a string, code that calls .getTime() on a parsed value fails at runtime; turn it back into a date where you parse it. For binary data, encode it as Base64 rather than letting a typed array become an object with numbered keys.

4. Python writes JSON that JavaScript can’t read

Python’s json.dumps writes NaN, Infinity and -Infinity by default. Its own documentation admits this “is not JSON specification compliant”, and JSON.parse rejects it with “Unexpected token N”. This typically shows up when a pandas or NumPy column with missing values is serialized straight into an API response. Use json.dumps(data, allow_nan=False) to get an error at the source, and convert missing values to None so they become null.

Python also escapes every non-ASCII character by default (ensure_ascii=True), so "café" becomes "café". That is valid JSON and decodes correctly, but it makes files larger and diffs harder to read. Pass ensure_ascii=False and write the file as UTF-8.

5. An invisible character at the start

RFC 8259 requires UTF-8 for JSON exchanged between systems and forbids adding a byte order mark, the invisible U+FEFF character some Windows editors put at the start of a file. Parsers may skip it, but JavaScript’s doesn’t: JSON.parse fails on the very first character of a file that looks perfectly fine in an editor. If a config file throws an “unexpected token” error at position 0, strip a leading \uFEFF before parsing, and save the file as UTF-8 without BOM.

6. A key called __proto__

JSON.parse treats "__proto__" as an ordinary key, which is safe on its own. The trouble starts when that parsed object is merged into another one with code that assigns properties, because assignment to __proto__ changes the target’s prototype:

const input = JSON.parse('{"__proto__": {"isAdmin": true}}')
const user = Object.assign({}, input)
user.isAdmin // true, inherited from the attacker's object

Recursive “deep merge” helpers have the same weakness, and some modify Object.prototype itself, which affects every object in the program. This is called prototype pollution. Validate untrusted JSON against a schema that lists the allowed keys, skip __proto__, constructor and prototype in merge code, or merge into Object.create(null). Object spread ({ ...input }) defines properties rather than assigning them, so it doesn’t have this problem.

7. Dates have no standard form

JSON has strings, numbers, booleans, null, objects and arrays, and nothing else. A date has to be one of those, and every API picks its own: ISO strings, Unix seconds, Unix milliseconds, or a local time with no zone. When you design an API, use ISO 8601 strings in UTC, such as "2026-10-17T09:30:00Z". They are readable, unambiguous and sort correctly as text. If you use numbers, name the unit: createdAtMs beats created. The year 2038 guide explains why the field that holds it must be 64-bit.

Checklist for an API that speaks JSON

  • IDs and any integer that may pass 253 as strings.
  • Money in minor units or decimal strings, never floats.
  • Reject duplicate keys at the edge of the system.
  • Missing values as null, never NaN.
  • Dates as ISO 8601 in UTC, or numbers with the unit in the name.
  • UTF-8 without BOM; schema validation before merging.

To compare what a service sent with what you expected, format both in the JSON formatter with sorted keys, then paste them into the text diff checker.

Frequently asked questions

What is the largest number JSON can hold?

The JSON grammar has no limit, but RFC 8259 warns that only integers between -(2^53)+1 and 2^53-1 are safe to exchange, because most parsers read numbers as 64-bit floating point. In JavaScript that limit is Number.MAX_SAFE_INTEGER, 9007199254740991. Send larger integers, such as 64-bit database IDs, as strings.

Can JSON have duplicate keys?

The grammar allows it, but RFC 8259 says names should be unique and that software receiving duplicates behaves unpredictably. JavaScript and Python keep the last value; some other parsers keep the first or reject the document. Treat duplicate keys as an error in anything you accept from outside.

Why does JSON.stringify remove some of my properties?

Properties whose value is undefined, a function or a symbol are left out of objects, and become null inside arrays. NaN and Infinity become null too. Maps and Sets turn into empty objects, and a BigInt throws a TypeError unless you convert it first.

How should dates be stored in JSON?

JSON has no date type, so pick a convention and stick to it. ISO 8601 strings in UTC, such as 2026-10-17T09:30:00Z, are readable and sort correctly as text. Unix timestamps work too, but say in the field name whether they are seconds or milliseconds.

Is NaN valid JSON?

No. RFC 8259 has no representation for NaN or Infinity. Python’s json module writes them anyway by default, as NaN and Infinity, which JSON.parse in JavaScript rejects. Pass allow_nan=False in Python to get an error instead, and send missing values as null.