Vaclav Kosar's face photo
Vaclav Kosar
Software And Machine Learning Blog

To What Python Number Types Does json.loads Parse?

JSON specifies only a number value, so how to infer the correct type between int and float?

How JSON handles numbers?

JSON ECMA leaves mapping of the human-readable format to correct language type to the programming language:

JSON is agnostic about the semantics of numbers. In any programming language, there can be a variety of number types of various capacities and complements, fixed or floating, binary or decimal. That can make interchange between different programming languages difficult. JSON instead offers only the representation of numbers that humans use: a sequence of digits. All programming languages know how to make sense of digit sequences even if they disagree on internal representations. That is enough to allow interchange.

How json.loads selects number types?

Here is a code snippet with its standard output below it:

import json
for v in json.loads("[2000, 20.0, 20.1, 1e6, NaN, Infinity, -Infinity]"):
    print(f"{v}: {type(v)}")

2000: <class 'int'>
20.0: <class 'float'>
20.1: <class 'float'>
1000000.0: <class 'float'>
nan: <class 'float'>
inf: <class 'float'>
-inf: <class 'float'>

Python documentation also explains that NaN, Infinity, -Infinity are not actually part of JSON standard, but that they are interpreted anyway:

It also understands NaN, Infinity, and -Infinity as their corresponding float values, which is outside the JSON spec.

The conversion is similar to conversion from a Python source code. The number becomes int only in case of non-scientific format without a dot. Otherwise you’ll get a float.

Read also on this blog how to wrap you resource creation and closure into context manager with-statement and catch exceptions there.

25 Aug 2020