Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to decode following string (dictionary as a string value to outer dictionary key) in Python as JSON?

t1 = 
     """
        {"abc": "{\"version\": \"1\"}"}
     """
ans = json.loads(t1)

Above code results in error as Python doesn’t like that I’m providing dictionary as a string value of key "abc".

Assuming I cannot change the input mentioned above, I want to be able to decode the above-mentioned string in Python as JSON, how can I solve this problem?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That’s not a valid json string. You need to lose the extra quotes. Only the entire thing needs to be a string, not each individual value.

In [1]: t1 = '{"abc": {"version": "1"}}'

In [2]: import json

In [3]: ans = json.loads(t1)

In [4]: ans
Out[4]: {'abc': {'version': '1'}}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading