Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 411
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 0
Kenil Vasani
Asked: December 11, 20202020-12-11T20:35:41+00:00 2020-12-11T20:35:41+00:00In: Python

TypeError: Object of type ‘float32’ is not JSON serializable

  • 0
This question already has answers here:

Convert numpy type to python

(4 answers)

Closed 2 years ago.

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
jsonnumpypython
  • 1 1 Answer
  • 9 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Rohit Patel

      Rohit Patel

      • 0 Questions
      • 98 Answers
      • 0 Best Answers
      • 0 Points
      View Profile
      Best Answer
      Rohit Patel
      2020-12-11T20:35:26+00:00Added an answer on December 11, 2020 at 8:35 pm

      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

      • 7
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      FATAL EXCEPTION: Firebase-Messaging-Intent-Handle — java.lang.NoClassDefFoundError

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com