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 in Python by kartik
• 11,290 points
115 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 by Niroj
• 22,790 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
1,611 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
• 57,530 points
1,029 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
• 57,530 points
107 views
0 votes
1 answer

How to convert a Pandas GroupBy object to DataFrame in Python

g1 here is a DataFrame. It has a hierarchical index, ...READ MORE

answered Nov 12, 2018 in Python by Nymeria
• 3,540 points
17,158 views
+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 947 views
0 votes
0 answers
0 votes
1 answer

How to convert dictionary to JSON?

Hello @kartik, json.dumps() returns the JSON string representation of ...READ MORE

answered Apr 15 in Python by Niroj
• 22,790 points
103 views
0 votes
1 answer

How to unzip a list of tuples into individual lists?

Hello @kartik, zip is its own inverse! Provided you ...READ MORE

answered Apr 23 in Python by Niroj
• 22,790 points
24 views