pythonjsonformatter

Preserve large IDs and decimal precision in JSON

Valid JSON does not guarantee that every consumer preserves the exact number you wrote. Choose the representation before formatting.

A large identifier can change during parsing

A database can export an integer that Python reads exactly but a browser cannot represent exactly as a JavaScript number. Standard JavaScript numbers use binary floating point. The largest positive safe integer is 9007199254740991; beyond the safe range, adjacent integers are not all distinguishable.

For example, a browser can read the number 9007199254740993 as 9007199254740992. The problem can happen before you format or compare it. If the value is an opaque account, event, or database identifier, transmitting it as a string is usually a clearer contract:

{"event_id":"9007199254740993","total":"19.90"}

Load the string-preserving example and press Convert. The ID and decimal text remain strings in the JSON result. Changing a rounded number into a string after parsing does not restore the lost digits; return to the original source instead.

Read decimal tokens deliberately in Python

Python's JSON decoder normally converts fractional number tokens to float. If downstream calculations require decimal arithmetic, choose Decimal while decoding the original text rather than constructing it from an already rounded float.

import json
from decimal import Decimal

raw = '{"price":19.90,"count":3}'
data = json.loads(raw, parse_float=Decimal)
print(type(data["price"]).__name__)
print(data["price"] * data["count"])

Expected output:

Decimal
59.70

This controls the Python consumer only. It does not require a browser or another language to use decimal arithmetic. Agree on the wire format with every consumer that needs the same precision.

Choose the output schema for an exact amount

The standard encoder does not serialize a Decimal automatically. If exact displayed decimal digits are part of the contract, convert that specific field to a string. Do not apply default=str to every unknown object: it can hide an unexpected type elsewhere in the payload.

import json
from decimal import Decimal

price = Decimal("19.90")
payload = {"price": str(price), "currency": "EUR"}
print(json.dumps(payload))

Output:

{"price": "19.90", "currency": "EUR"}

The receiver must now treat price as decimal text, not as a number without validation. Integer minor units are another possible contract when the application's scale is fixed, but the unit and allowed range must be explicit. Merely adding more printed decimal places cannot recover precision already lost.

When this browser tool is the wrong checkpoint

The converter, Viewer, and JSON comparison operate on JavaScript values. They are useful for shape and syntax inspection, but they are not a lossless numeric validation pipeline for arbitrary large integers or exact decimals. A diff can miss a distinction if both original number tokens round to the same value.

Keep the original payload and use a parser with the required numeric model for correctness checks. For a raw textual audit, retain the exact source bytes as well. See RFC 8259's number interoperability discussion and Python's Decimal documentation.

By Python JSON Formatter ·