How To Resolve “TypeError: Object Of Type Float32 Is Not JSON Serializable” In Python

How To Resolve TypeError: Object Of Type Float32 Is Not JSON Serializable In Python

The error “TypeError: Object of type float32 is not JSON serializable” happens when you convert a float32 object to a JSON string. To fix the error, let follow the article below.

Javascript Object Notation (JSON)

JSON (Javascript Object Notation) is a standard format used to exchange and store information between processes in Python. JSON is part of JS but is now developed, not in JS or any other language. JSON data types include:

  • object
  • array
  • string
  • number
  • boolean
  • null

Serialization is converting the above objects into a form that can be understood by the computer so that they can be stored and transmitted. Since Python 2.6 module ‘json’ has been added, you must convert the Python data type to the corresponding Json type during the serialization process.

What causes the error “TypeError: Object of type float32 is not JSON serializable”?

TypeError: Python throws this error when you perform an unsupported operation on an object with an invalid data type. 

The error happens when you convert a float32 object to a JSON string because float32 is not Python’s data type and can be converted to JSON’s data type.

Example:

import numpy as np
import json

pi = np.float32(3.1415)
print(json.dumps(pi))

Output:

TypeError: Object of type float32 is not JSON serializable

How to solve this error?

Use the str() method

You can use the str() method to solve this error.

Syntax:

str(object)

Parameters:

  • object: is an object that can be displayed as a string.

The str() function returns a string.

Example:

import numpy as np
import json

pi = np.float32(3.1415)

# Use the str() method to convert 'numpy.float32' to string
newStr = str(pi)

# Use the json.dumps() function to convert it to a json string
print(json.dumps(newStr))

Output:

"3.1415"

In the above example, I import the ‘json’ module and the ‘numpy’ module in the above example. I convert the float32 object to the string using the str() function, then use json.dumps() function to convert the dictionary to json string.

Convert the float32 object to a number

You can convert a float32 object to a number by the following syntax:

json.dumps(float32 object.item())

Example:

import numpy as np
import json

pi = np.float32(3.1415)

# The float32 object convert to a number by function json.dumps() 
print(json.dumps(pi.item()))

Output:

3.1414999961853027

In the above code, I convert the float32 object to a number, and the problem is solved.

Use the float() method

The float() function will convert the float32 object to a floating point, and the error will be resolved.

Syntax:

float(x)

Parameters:

  • x: object to be converted to floating point.

Example:

import numpy as np
import json

pi = np.float32(3.1415)

# The float32 object convert to floating point number by the float() function
floatVar = float(pi)
print(json.dumps(floatVar))

Output:

3.1414999961853027

In the above code, I import the ‘json’ module and the ‘numpy’ module. I do convert the float32 object to the floating point number using the float() function. Finally, use json.dumps() function to convert the dictionary to json string.

Summary

If you have any questions about the “TypeError: Object of type float32 is not JSON serializable” error in Python, leave a comment below. I will answer your questions. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *