Checking the source spelling of negative zero in JSON
The formatter writes a number back the way it was typed, so -0 stays -0 and 0 stays 0. This worksheet checks the two tokens and explains why the sign of zero is a question for the program that reads the data.
Two spellings of zero
In mathematics there is one zero, and negative zero is the same number as zero. In the IEEE 754 floating-point arithmetic used by most computers there are two bit patterns, positive zero and negative zero, that compare as equal but behave differently in a few operations. Dividing one by negative zero gives negative infinity, and dividing one by positive zero gives positive infinity. A comparison of the two with an equality test returns true, so most programs never notice the sign unless they look for it. The sign also affects the output of a few functions, such as the arctangent of two arguments, whose result depends on which zero it receives. JSON grammar allows a minus sign before a zero, so -0 is a valid number token. The worksheet places -0 and 0 in the same property position to see what a formatter does with them.
What the formatter returns
With the input {"value":-0} and an indent of zero, the output is {"value":-0}. With {"value":0} the output is {"value":0}. Both are valid, and the formatter keeps each number token exactly as written instead of converting it to a number and printing it again. A tool that parsed the value and re-serialized it might drop the sign, since many serializers print negative zero as 0. This formatter works on the text of the token, so the minus sign survives, and a file comparison can tell the two outputs apart. Both documents also remain the same length apart from that one character, which is why a careless reader can skim past the difference.
Why a token-preserving formatter matters
A document that passes through a formatter should come out with the same data it went in with. If formatting rewrote numbers, then a long integer might lose digits, a value written as 1.50 might become 1.5, and -0 might become 0. Each of these changes the text that a downstream consumer sees. Preserving tokens keeps the formatter in the role of changing whitespace only. That role makes it safe to use on documents that are stored, reviewed in a version control diff, or signed, because a reformat touches layout lines and nothing in the data values. For -0 the preservation is easy to verify, since the one visible difference is the minus sign.
What readers of the data may do
After the data leaves the formatter, the consumer decides what -0 means. JavaScript's JSON.parse turns the token -0 into negative zero, while JSON.stringify of negative zero writes 0, so a round trip through that language drops the sign. Some other languages read the integer token -0 as an ordinary integer zero with no sign at all, and may keep a negative sign only for a float spelled -0.0. The JSON specification itself says nothing about the machine representation, which is left to the receiving software and its numeric contract. RFC 8259 notes that implementations differ in the range and precision of numbers they accept, and recommends that senders avoid depending on more than double-precision behavior if they want predictable results.
Where signed zero shows up in practice
Negative zero can arise from rounding a small negative number to zero, from multiplying zero by a negative value, or from an instrument that reports a sign for a reading below its resolution. Charting libraries, scientific computation and some graphics routines can treat the sign as meaningful, for example in branch cuts of complex functions or in the direction of a limit. Most business data does not care. A report listing a temperature change as -0 may confuse readers, though, and many teams normalize such values to 0 before display. Spreadsheets and statistical packages differ in whether they show the sign, so the same underlying value can display as 0 in one tool and -0 in another. If a number is going to a human reader, formatting it for display is a separate step from storing it.
Related spellings that are also preserved
The same principle covers other spellings of the same value. The formatter keeps 1.50 as 1.50, keeps -0.0 as -0.0, and keeps 1e3 as 1e3. Each of these spellings parses to the same or a similar number as a simpler form, yet the token text differs. If you need a single canonical spelling for comparing or signing documents, a separate canonicalization step with explicit rules is required, because pretty-printing alone does not produce one. RFC 8785, the JSON Canonicalization Scheme, is one published set of such rules, and it prints numbers in a defined way that would turn -0 into 0. Use a canonical form only where the consuming format asks for it.
Comparing files that differ only in a sign
If two files differ only by a minus sign on a zero, a text comparison will flag a difference that a numeric comparison would ignore. That is useful when checking that a pipeline left a source untouched. It is noise when comparing outputs from two systems that spell zero differently. Decide beforehand whether the comparison should be textual or numeric, and normalize both sides to the same spelling when only the numeric value matters.
Lexical behavior of two number tokens
The worksheet shows the formatter's lexical behavior for two number tokens. It makes no claim that every parser, serializer or downstream calculation preserves the sign in the same way, and it does not say whether your application should accept, reject or normalize a negative zero.
Normalizing negative zero when it matters
If your own code produces JSON and you want to avoid -0 in the output, add zero to the value before serializing, since negative zero plus positive zero equals positive zero in IEEE 754 arithmetic. A rounding routine can also be followed by a check that replaces a result equal to zero with a literal zero. On the reading side, a comparison such as value equals zero is true for both signs, so ordinary equality tests need no change. Only code that inspects the sign bit or divides by the value can tell the difference. Writing down which of these your system does saves a later search. A short test that serializes -0 and reads the output back is enough to document the behavior of your language and library version. Keep the test in the project so that a library upgrade that changes the behavior is caught the next time the suite runs.
Reproduce the checks
Negative-zero source token
Open the Json Validator Formatter and enter:
- JSON input
{"value":-0}- Indent
0
Expected output:
Valid
trueFormatted
{"value":-0}The source minus sign remains in place.
Ordinary zero control
Open the Json Validator Formatter and enter:
- JSON input
{"value":0}- Indent
0
Expected output:
Valid
trueFormatted
{"value":0}Formatting does not make the two token strings identical.
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