Python Forum

Full Version: Change string into Dict
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
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()