Skip to content

Convert a String representation of a Dictionary to a dictionary โ€‹

You can use the json module in Python to convert a string representation of a dictionary to a dictionary. Here's an example:

Convert a dictionary to a JSON string in Python

python
import json

string_representation = '{"key1": "value1", "key2": "value2"}'
dictionary = json.loads(string_representation)

print(dictionary)
# Output: {'key1': 'value1', 'key2': 'value2'}

<div class="alert alert-info flex not-prose"> Watch a course Python - The Practical Guide</div>

In the example above, json.loads() is used to convert the string representation of the dictionary to a dictionary. The output of the print statement is the dictionary itself.

Do you find this helpful?

Dual-run preview โ€” compare with live Symfony routes.