Python Forum
Change string into Dict - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Change string into Dict (/thread-9871.html)



Change string into Dict - Robin_at_Cantelli - May-02-2018

Hey,
I'm getting data from the web which is basically a dictionary. The problem is that one key refers to another dictionary which is unfortunately a string:

dict = {'somethin':'else','data':'{the other dict}',.......} 
for obvious reasons i want to change it to change it to:

{'somethin':'else','data':{the other dict},.......}
in words that '{the other dict}' goes to {the other dict}

Has someone an idea to do that quick and simple?

thanks in advance

Robin


RE: Change string into Dict - gruntfutuk - May-02-2018

A lot of data obtained over the web is obtained in JSON (JavaScript object notation) format, which include nesting of dictionaries in the manner you described. Not sure from your fragment if this is the case here, but it if is, you will be pleased to know there's a library just for this. A quick google of python JSON will lead you to good solution guides.


RE: Change string into Dict - vishalhule - Mar-05-2020

Just to demonstrate what you can do using Python json module.

sampleObj = {
  "car1": { "name":"Ford", "price":35000},
  "car2": { "name":"BMW", "price":65000}
 }

str = json.dumps(sampleObj, indent=4)
print(str)
use the json.dumps()