Python's nonstandard JSON encoding

by
, posted

This was last updated for Python 3.11.4.

Python’s built-in JSON module, json, can produce results that most other JSON parsers do not accept. This is because it has nonstandard serializations for infinity, negative infinity, and NaN.

import json
import math

json.dumps([math.inf, -math.inf, math.nan])
# => "[Infinity, -Infinity, NaN]"

Most other languages and libraries can’t decode this. For example, JSON.parse("Infinity") throws an error in JavaScript.

To avoid this, you can pass the allow_nan option, which will throw an error if you try to serialize these values. (Though it’s called “allow NaN”, it also throws if passed inf.)

json.dumps(math.nan, allow_nan=False)
# ValueError: Out of range float values are not JSON compliant

json.dumps(math.inf, allow_nan=False)
# ValueError: Out of range float values are not JSON compliant

I tested the following languages and libraries, all of which fail to parse these values:

However, there are a few places that parse these just fine:

These values are also valid JSON5. JSON5 “is not intended to be used for machine-to-machine communication”, but can parse the values “Infinity”, “-Infinity”, and “NaN”.

I found Python’s behavior pretty surprising, so I thought I’d write it up!