How to convert a DictProxy object into JSON serializable dict

0 votes

I have a DictProxy object created using multiprocessing.Manager().dict() to support concurrency. At the end of the run, I need to serialize the dict to JSON. But it's unclear how to convert the DictProxy to serializable dict object. When I tried it, I got:

TypeError: <DictProxy object, typeid 'dict' at 0x10a240ed0> is not JSON serializable

How can I fix this?

Apr 15, 2020 in Python by kartik
• 37,490 points
913 views

1 answer to this question.

0 votes

Hello,

Rather than using a private DictProxy method like _getvalue(), I'd prefer using a public one like copy() which returns a shallowly-copied dict.

import multiprocessing

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

Hope this work!

Thank You!!

answered Apr 15, 2020 by Niroj
• 82,560 points

Related Questions In Python

0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
2,167 views
0 votes
1 answer

How to merge multiple json objects into a single json object using python

You may do like this but it ...READ MORE

answered Nov 22, 2020 in Python by Gitika
• 65,870 points
762 views
0 votes
1 answer

Convert string list of dict from csv into JSON object in python

You can use the ast module Ex: import ast s = """[{'10': ...READ MORE

answered Sep 12, 2018 in Python by Priyaj
• 58,100 points
1,493 views
0 votes
1 answer

Convert string list of dict from csv into JSON object in python

You can use the ast module Ex: import ast s = """[{'10': ...READ MORE

answered Sep 24, 2018 in Python by Priyaj
• 58,100 points
181 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,851 views
0 votes
0 answers
0 votes
1 answer

How to convert JSON data into a Python object?

Hello @kartik, You can do it in one ...READ MORE

answered Aug 3, 2020 in Python by Niroj
• 82,560 points
494 views
+1 vote
1 answer

How to convert a PIL Image into a numpy array?

Hello @kartik, You just need to convert your ...READ MORE

answered Jun 22, 2020 in Python by Niroj
• 82,560 points
1,470 views