Representing a leading-zero identifier as a JSON string
An identifier such as 00123 survives in JSON only as a string. This worksheet formats the quoted form, shows the unquoted form being rejected, and explains the grammar rule behind the difference.
The task: keep all five characters
Some identifiers are made of digits and still are not numbers. A part number, a postal code, an invoice reference or a locker label can start with zeros that are part of its name. Written into JSON, such a value has two possible spellings. One is the string "00123" in double quotes, which holds five characters. The other is the bare token 00123, which looks like a number with extra zeros in front. The JSON validator formatter accepts the first and rejects the second. The worksheet shows both results next to each other, because the rejection is the useful warning. It tells you before the data leave your hands that the second spelling is not JSON at all, and that any program which seems to accept it is doing something outside the standard. A quick way to see the difference is to count characters. The quoted identifier has five characters between its quotation marks, while the number it superficially resembles would be read as 123, with three. Any step in a pipeline that changes five characters into three has altered the identifier.
What the quoted form does
In the control that passes, the document is an object with the name id and the string value 00123. Formatting with an indentation of zero returns the same compact text, and the validity flag is true. The string is stored exactly as written, so a program that reads it back receives five characters, the three digits 123 preceded by two zeros. Nothing in JSON treats the contents of a string as a quantity. The digits are just characters, equal in rank to letters, so the leading zeros carry the same status as any other character. This is the reason strings are the recommended container for codes. They make no assumptions about the meaning of what they hold. Sorting also behaves differently. As text, the strings 00123 and 00124 sort in numeric order because they share a width, whereas the unpadded strings 9 and 10 would sort in the wrong order. Fixed-width padding is the reason some identifiers keep their zeros.
What the unquoted form does
In the second document the value is the bare token 00123. The formatter returns an error that the JSON syntax is invalid. The grammar for numbers in RFC 8259 allows an integer part that is either a single zero or a nonzero digit followed by any digits. A zero followed by other digits is therefore not a valid number. The rule exists to prevent ambiguity, since in several programming languages a leading zero marks an octal literal. By forbidding the form entirely, JSON avoids a situation in which one parser reads 00123 as 123 and another as the octal value 83. The rejection is intentional and consistent across conforming parsers. The error message names the syntax, not the number, which is a hint that the problem is the spelling and not the value. The fix is to change the spelling by quoting, or to remove the zeros only if the field is a quantity.
Why not simply drop the zeros
It might seem harmless to rewrite 00123 as 123 and move on, since that is a valid JSON number. The change alters the identifier. In a system that compares codes as text, 123 and 00123 are different keys, and a lookup that worked before will now miss. Even where the receiving system tolerates both, the width may carry meaning, such as a sequence number padded to five places so that sorting by text matches sorting by order. Removing zeros destroys that property. Repairing a rejected value by stripping its zeros is a data change, and someone responsible for the data should approve it, not a formatter. A related risk is a database key that treats 00123 and 123 as one value in a numeric column but as two values in a text column. Moving data between those column types can merge records that were separate.
Other number rules in the grammar
The leading-zero restriction is one of several rules that make JSON numbers stricter than those in many languages. A number may have a leading minus sign but not a plus sign. A decimal point must have digits on both sides, so .5 and 5. are invalid and 0.5 is valid. Hexadecimal forms, the special values NaN and Infinity, and thousands separators are all outside the grammar. The single digit 0 is valid, and so is -0. Knowing these rules explains many formatter errors. A value that looks like a harmless number to a person, such as +1 or 007, is rejected because it does not fit the grammar exactly. Exponent notation is allowed, so 1e3 is a valid number, but it reads as a quantity of one thousand. An identifier written that way would be unrecognizable, which is another reason to quote codes.
Numbers lose digits in some parsers
A second reason to prefer strings for identifiers concerns size. Many JSON parsers, JavaScript among them, read numbers into double-precision floating point, which represents integers exactly only up to 9,007,199,254,740,991. A 17 digit account number written as a JSON number may come back changed in its last digits. For this reason, APIs that issue large numeric identifiers often send them as strings. Leading zeros and large values are two separate hazards with the same cure. If the field is a label, store it as text, and the digits arrive exactly as sent. Languages with arbitrary-precision integers can read long numbers exactly, and exchanging data with systems that cannot is the point at which silent changes appear.
A second identifier example
A postal code is the classic case. In the United States, ZIP codes in parts of the Northeast start with 0, so a code such as 02134 must be stored as the five character string. The document with the member zip holding the quoted text 02134 formats and validates. The same document with the bare token 02134 is rejected for the same reason as 00123. The numbers here are illustrative of the pattern and are not meant to name any address. Telephone numbers behave in the same way. A number that begins with the digit 0 as a national dialing prefix would lose it if converted to a numeric value. Product codes illustrate the same pattern. A code made of a country prefix of 00 followed by digits would turn into a shorter number if stored numerically, and two different products could end up sharing a value.
Matching the receiving schema
The formatter cannot decide whether a field should be a string or a number. That decision belongs to the schema or interface description of the receiving system. A good schema states the type of each field and, for string identifiers, may state a pattern, such as exactly five digits. When two teams exchange data, agreeing on this up front prevents silent conversion later. If the schema says the field is a string, producers must quote the value. If it says number, producers must not use leading zeros, and the width, if it matters, has to be recreated by formatting at display time. Where the schema is described in a formal language, a validator can enforce the rule automatically, rejecting numbers in a field declared as a string before they reach the application.
CSV and other sources
The leading zeros are often lost earlier in the pipeline than the JSON step. A spreadsheet that opens a CSV file may interpret a column of digits as numbers and strip the zeros before anyone exports anything. The linked CSV guide on this site shows how a text-preserving conversion keeps each cell as a string. After that, the JSON step is straightforward, because the value is already text and only needs to be quoted. Fixing the problem at the source is cheaper than trying to restore zeros afterward, since the original width may no longer be known. The same applies to data pasted from documents. A word processor or web page may show a code with a leading apostrophe or a text format marker that is not part of the value, and it should be removed deliberately.
Padding for display
Sometimes the stored value is a true number and only the display needs zeros. An order counter stored as 123 can be shown as 00123 by padding to five places when a page or a label is produced. That keeps the data compact and numeric and moves the width into the presentation. The decision between storing a padded string and storing a number plus a display rule depends on whether the padding is part of the identity of the item. If printed documents and databases must match character for character, store the string. If the padded width might change, say from five digits to six, keep the width as a configuration value and not as a literal scattered through code.
What the formatter checks and what it does not
The tool parses strict JSON, then writes the result at the requested indentation. It confirms the grammar. It does not confirm that an id is unique, correctly padded, present in a database or of the right type for the consumer. A clean result for the quoted document says that the text is valid JSON, and the failure for the unquoted one says that it is not. Those are the facts established by the two controls on this page. Everything else about the identifier is a matter of agreement between the people and systems who use it. It also keeps the order and spelling of what you gave it, so a string identifier comes out exactly as it went in, which makes it a safe tool for inspecting suspect data.
Characters that need escaping inside an identifier
Most identifiers are plain digits and letters, but some contain characters that JSON strings must escape. A double quotation mark inside the value is written with a backslash in front of it, a backslash is written as two backslashes, and control characters such as a tab or a line break cannot appear raw and must be written as escapes like backslash t or backslash n. An identifier read from a label scanner may carry a trailing newline that nobody notices on screen. If that newline is not escaped, the document is invalid, and if it is escaped, the identifier no longer equals the visible text. Trimming the value before it is written, and recording that it was trimmed, avoids both outcomes.
Reproduce the checks
Identifier as text
Open the Json Validator Formatter and enter:
- JSON input
{"id":"00123"}- Indent
0
Expected output:
Valid
trueFormatted
{"id":"00123"}All source digits are preserved.
Leading-zero number token
Open the Json Validator Formatter and enter:
- JSON input
{"id":00123}- Indent
0
Expected rejection:
Invalid JSON syntaxThis spelling is not accepted as a JSON number.
References and implementation scope
The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.
Sources
- Primary definition and convention reference ↗ · 2026-10-08