Why did a price of 45.50 turn into 45.5 after formatting my JSON file? Did the value change?
No, the value is the same. 45.50 and 45.5 are the same number, and the JSON formatter parses your text into numbers and writes them back in their shortest form, so trailing zeros after the decimal point go: 45.50 becomes 45.5 and 120.00 becomes 120. If your system needs the exact written form, say two decimals, store the price as a string in quotes or format it where it is displayed.
The example
A line from a price file:
{"price":45.50,"old":120.00}
After minifying in the tool:
{"price":45.5,"old":120}
The size dropped from 28 bytes to 24, and the whole difference is zeros and a decimal point. Formatting with 2 spaces does the same: the price shows as 45.5 and the old price as 120.
Why it happens
The tool does not reformat your text character by character. It reads it with the browser's real JSON parser, which turns 45.50 into a number in memory. A number in memory does not remember how it was written; it has no idea the author added a trailing zero. When the tool writes the data back, each number is written in the shortest form that gives the same value. More from the same test: 1.10 becomes 1.1, 0.150 becomes 0.15, and 3.0e2 becomes 300.
This is not a bug in the tool; it is what any program does when it reads JSON and writes it out again. The difference is that the tool states it plainly in its limitations, and you have now seen it before it surprises you somewhere else.
Does it matter to you?
- Not if the system receiving the file reads the price as a number: 45.5 and 45.50 are one thing to it, and it will display the value however its settings say.
- Yes if the system shows the text as is, without formatting, so a customer sees 45.5 SAR instead of 45.50.
- Yes if you compare the file before and after formatting character by character, because every such price will look changed.
The fix: price as text when the format matters
If you need the exact written form, make the price a string in quotes at the source:
{"price":"45.50","old":"120.00"}
Now the tool changes nothing, 32 bytes before and after, because strings stay exactly as written. Make sure the receiving system accepts a price as text, though; some insist on a number. Check the documentation of the system you are sending to.
Steps to check
- Open the JSON formatter and validator and paste the file.
- Choose 'Format' and look at the prices in the output: any number that ended in a zero will appear without it.
- Decide: if the system reads numbers, do nothing; if it shows the text as is, change how prices are written at the source.
If the prices started in a spreadsheet and you converted them to JSON with the CSV and JSON converter, note that it keeps values as strings by default, so "45.50" stays as written unless you turn on number conversion there.
Common questions
Can the value itself ever change?
Not for ordinary prices. A real change of value happens with very long integers above 9007199254740991, and the tool warns you about those.
Does the tool drop zeros inside strings too?
No. Anything inside quotes stays exactly as it is.
Can I switch this behaviour off?
No, it is part of writing the data back. The only fix is to have the price as a string at the source.
