Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

TypeError: Object of type DictProxy is not JSON serializable

import multiprocessing, json
class JSONEncoderWithDictProxy(json.JSONEncoder):
	    def default(self, o):
             if isinstance(o, multiprocessing.managers.DictProxy):
                     return dict(o)
             return json.JSONEncoder.default(self, o)

json.dumps(your_nested_d, cls=JSONEncoderWithDictProxy)
Comment

TypeError: Object of type DictProxy is not JSON serializable

If the dict values are all serializable
It looks like simply passing the DictProxy through the dict constructor allows you to serialize the data to JSON. The example below is from Python 3.6:

>>> import multiprocessing, json
>>> m = multiprocessing.Manager()
>>> d = m.dict()
>>> d["foo"] = "bar"
>>> d
<DictProxy object, typeid 'dict' at 0x2a4d630>
>>> dict(d)
{'foo': 'bar'}
>>> json.dumps(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  ...
TypeError: Object of type 'DictProxy' is not JSON serializable
>>> json.dumps(dict(d))
'{"foo": "bar"}'
As you can see, while d is a DictProxy, using json.dumps(dict(d)) instead of json.dumps(d) allowed the data to be serialized. If you are using json.dump, the same applies.

If some dict values are also DictProxies
Unfortunately, the above method does not work if a value in the DictProxy is also a DictProxy. Such a value is created in this example:

>>> import multiprocessing
>>> m = multiprocessing.Manager()
>>> d = m.dict()
>>> d["foo"] = m.dict()
The solution is to extend the json.JSONEncoder class to handle DictProxy objects, like so:

>>> import multiprocessing, json
>>> class JSONEncoderWithDictProxy(json.JSONEncoder):
...     def default(self, o):
...             if isinstance(o, multiprocessing.managers.DictProxy):
...                     return dict(o)
...             return json.JSONEncoder.default(self, o)
...
>>> m = multiprocessing.Manager()
>>> d = m.dict()
>>> d["foo"] = m.dict()
>>> d["foo"]["bar"] = "baz"
>>> json.dumps(d, cls=JSONEncoderWithDictProxy)
'{"foo": {"bar": "baz"}}'
>>> # This also works:
>>> JSONEncoderWithDictProxy().encode(d)
'{"foo": {"bar": "baz"}}'
Comment

TypeError: Object of type DictProxy is not JSON serializable

import multiprocessing

if __name__ == '__main__':
    manager = multiprocessing.Manager()
    d = manager.dict()
    import json
    json.dumps(d.copy())
Comment

PREVIOUS NEXT
Code Example
Python :: python function pointer with multiple args 
Python :: Python - Comment Parse String to List 
Python :: landscape odoo report 
Python :: why is python so populair 
Python :: python - dashboard 
Python :: pdf reading shows gibberish python 
Python :: select columns rsnge dataframe 
Python :: for 2d data compute standard deviation at each x 
Python :: airflow find trigger type 
Python :: if boolean func 
Python :: restart device micropython 
Python :: python quickly goto line in file 
Python :: #Combine two sets on python with for loop: reverse way in one line with space 
Python :: useful functions in python 
Python :: save csv with today date pandas 
Python :: treesitter python languages 
Python :: concatenating ols model results 
Python :: python exception vs error 
Python :: variable types in python 
Python :: dependency parser tags 
Python :: django drf endpoint without model 
Python :: check if there is a certain number difference with python 
Python :: jet 4 access python password 
Python :: python how to request query string korean encode 
Python :: converting 4hr 20min to minutes 
Python :: python how to get variable value in dict 
Python :: qtile: latest development version 
Python :: fibonacci numbers function python print 
Python :: get the values of your aws tags from ec2 instance 
Python :: convert unit dynamo revit 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =