Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Object of type 'float32' is not JSON serializable [duplicate]

Tags:

python

json

numpy

I'm working with numpy.float32 numbers and they don't go into JSON. What's the right approach to overcome this issue?

import numpy as np import json  a = np.float32(1) json.dumps(a)  TypeError: Object of type 'float32' is not JSON serializable 
like image 512
Franco Piccolo Avatar asked Oct 31 '18 11:10

Franco Piccolo


People also ask

How do I fix the object of type datetime is not JSON serializable?

The Python "TypeError: Object of type datetime is not JSON serializable" occurs when we try to convert a datetime object to a JSON string. To solve the error, set the default keyword argument to str in your call to the json. dumps() method.

How do I make an object JSON serializable?

Use toJSON() Method to make class JSON serializable So we don't need to write custom JSONEncoder. This new toJSON() serializer method will return the JSON representation of the Object. i.e., It will convert custom Python Object to JSON string.

How do you serialize an object to JSON in Python?

The json module exposes two methods for serializing Python objects into JSON format. dump() will write Python data to a file-like object. We use this when we want to serialize our Python data to an external JSON file. dumps() will write Python data to a string in JSON format.

Is not JSON serializable Python error?

The Python "TypeError: Object of type dict_items is not JSON serializable" occurs when we try to convert a dict_items object to JSON. To solve the error, convert the dict to JSON or pass the dict_items object to the list() class before the conversion to JSON.


1 Answers

It has to be a string, so you can have:

json.dumps(str(a)) 

EDIT:

JSON is a format for serialising object data. It doesn't really care or know about Python types, the json package tries to translate whatever object you pass json.dumps() into a string form via a conversion table that only supports some types (see doc below).

This is the reason why I think it's a good idea to just pass a string to avoid this issue: numpy.float32 just isn't in the table.

Because some have commented that explicitly passing a string to dumps "sounds wrong" I'll just add the doc here

json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw) Serialize obj to a JSON formatted str using this conversion table. The arguments have the same meaning as in dump().

Note Keys in key/value pairs of JSON are always of the type str. When a dictionary is converted into JSON, all the keys of the dictionary are coerced to strings. As a result of this, if a dictionary is converted into JSON and then back into a dictionary, the dictionary may not equal the original one. That is, loads(dumps(x)) != x if x has non-string keys.

taken from the official docs here: https://docs.python.org/3/library/json.html

like image 82
vencaslac Avatar answered Sep 21 '22 07:09

vencaslac