Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python json module represent floats in dictionary keys as strings?

See the following code:

>>> import json
>>> m = {}
>>> m[0.0] = 1.0
>>> json.dumps(m)
'{"0.0": 1.0}'

In the value of the JSON, we have 1.0. But in the key we have "0.0" (a Json string).

This ambiguous handling of floats just cost me some debugging time. Does anyone know why python's json module does this?

like image 755
Max Avatar asked Sep 01 '25 20:09

Max


1 Answers

Because a JSON key must be a string. See the RFC.

like image 188
Thanatos Avatar answered Sep 05 '25 02:09

Thanatos