Common JSON Syntax Mistakes and Numeric Precision Limits

Discover the strict JSON syntax rules regarding trailing commas and comments, plus how to handle large integer precision limits within JavaScript environments.

JSON is a strict subset of JavaScript syntax, though it behaves with a rigidity that often surprises those used to the flexibility of modern programming languages. It requires double quotes for all keys. Single quotes are not permitted. If you omit them or use the wrong ones, the parser fails immediately.

This rigidity extends to trailing commas. A comma after the last element in an array or object is forbidden. While JavaScript objects allow this to make version control diffs cleaner, JSON does not. You cannot leave a trailing comma and expect the file to be valid. This small detail causes frequent failures in automated pipelines where scripts append data without cleaning up the final character.

The specification also refuses to include comments. There is no syntax for a hash or a double slash. If you add a note to your configuration file, you break the format. This omission forces you to keep metadata in separate files or create dummy keys that act as labels but consume memory and complicate the object structure.

The Numeric Limit

JSON defines only one number type. It does not distinguish between integers and floating point numbers. Every digit sequence is treated as a generic number until the environment reading it decides otherwise. This simplicity creates a significant problem when you move data into a JavaScript environment.

JavaScript treats all numbers as double precision sixty-four bit floats. The safe integer range in JavaScript ends at two to the power of fifty-three minus one. If your JSON contains an ID larger than this, such as a sixty-four bit database primary key, the browser will round it to the nearest representable value.

You lose precision and corrupt your data without any warning. The parser does not throw an error; it simply provides the closest float. This is why you must send large IDs as strings. By wrapping a large integer in double quotes, you bypass the floating point limitation of the JavaScript engine. Once the string arrives at its destination, you can convert it to a BigInt to maintain accuracy.

Duplicate Keys and Parser Chaos

The JSON specification does not explicitly forbid duplicate keys within a single object. You can define the same key twice in one block without violating the core rules of the format. However, the specification is silent on how a parser should resolve this conflict. It leaves the decision entirely to the implementation.

This leads to an unpredictable environment. Some parsers will take the first value they encounter and ignore any subsequent entries for that key. Other parsers will take the last value found, effectively overwriting previous occurrences. If you send an object with two identical keys, the final state of your data depends on which library is reading it at the other end.

This inconsistency can lead to critical bugs in API integrations where different services use different parsing libraries. You should avoid duplicate keys entirely. Validating your JSON before transmission ensures that each key is unique. If you need multiple values for a single identifier, you must use an array of values instead of repeating the key. This removes the ambiguity and prevents the risk of different systems interpreting your data in opposite ways.

Try it: JSON formatter and validator

Sources

Every link below is checked before this page is published.

  1. RFC 8259 - The JavaScript Object Notation (JSON) Data Interchange Format
  2. MDN - JSON.parse()

More from the Format Notebook