TypeError: Object of type {Type} is not JSON serializable

Issue

I’m noting that the methods I am looking at to serialize a variable into JSON in python don’t really seem to handle it all that well, and for my purpose I just want to quickly dump an objects contents into a string format so I can pick out what I actually want to write custom code to handle. I want to be able to dump the main fields at the very least of any class I pass the python serializer and really if its worth the name this should work.

So take the following code:

import json
    
c = SomeClass()
 

#causes an error if any field in someclass has another class instance.   
json.dumps(c) 

leads to..

TypeError: Object of type {Type} is not JSON serializable

Are there any modules other people have used that would solve my problem ? I really don’t see how there would not be. Or maybe one might explain how to circumvent this error ?

The goal is to simply get some output to look at. If I wrote a recursion loop in c# using reflection, excepting circular references, it wouldn’t be difficult, so I cannot imagine python users have never tackled this exact issue and I’m not satisfied with the answers that I have seen in older posts which seem to suggest a lot of custom tinkering for something seems to be designed in spirit to just dump any old object’s contents out.

I don’t even need complex traversal is the funny part, though it would be nice. I just need a dump of the property values which are primitive types in many cases. I know this is possible because the debugger does it.

Additionally I looked at one of the methods given indicating to use default lambda to specify how the json serializer should descend into the object:

json.dumps(o, default=lambda k: k.__dict__)

and the object does not contain the standard dict member.

in the end I just ended up writing a class to do this.

Solution

edit:

Here use this now you can one way serialize a class structure with this nifty little bit of code that I added to address my problem with f**** discord.py !

end edit

There is no fire and forget option that would disentangle a mass of information.

The way of creating this solution would be to manage seperate lists of subclasses to make sure not to recurse until a stackoverflow is reached.

The slots_ can be used with getattr(o,name) when hasattr(o,’dict‘) is False.

But the answer is you’d have to create a solution that basically does the job that the json serializer should be doing and cut out circular reference by determining the unique complex types and writing them in seperate tabular entries in the json file and replacing them in the referencing classes with ids.

That way you could cross reference these objects while glancing at them.

However the short answer is no. Python does not offer an out of the box way of doing this and all the provided answers encountered thus far only solve a single use-case or scenario, and do not create a incorporated solution to the problem which the above mentioned algorithm WOULD by NORMALIZING the class data into unique elements.

Answered By – John Sohn

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published