Convert string representation of Dictionary to Dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Aug 31, 2022

banner

Photo from Unsplash

Convert string representation of Dictionary to Dictionary in Python #

Use the ast.literal_eval() method to convert the string representation of a dictionary to a dictionary, e.g. my_dict = ast.literal_eval(my_str). The ast.literal_eval() method allows us to safely evaluate a string that contains a Python literal.

main.py
from ast import literal_eval import json my_str = '{"id": 0, "name": "Alice", "salary": 100}' # ✅ convert string representation of dict to dict (ast.literal_eval()) my_dict = literal_eval(my_str) print(my_dict) # 👉️ {'id': 0, 'name': 'Alice', 'salary': 100} print(type(my_dict)) # 👉️ <class 'dict'> # -------------------------- # ✅ convert string representation of dict to dict (json.loads()) my_dict = json.loads(my_str) print(my_dict) # 👉️ {'id': 0, 'name': 'Alice', 'salary': 100} print(type(my_dict)) # 👉️ <class 'dict'>

The first example uses the ast.literal_eval() method to convert the string representation of a dictionary to an actual dictionary.

main.py
from ast import literal_eval my_str = '{"id": 0, "name": "Alice", "salary": 100}' my_dict = literal_eval(my_str) print(my_dict) # 👉️ {'id': 0, 'name': 'Alice', 'salary': 100} print(type(my_dict)) # 👉️ <class 'dict'>

The ast.literal_eval method allows us to safely evaluate a string that contains a Python literal.

The string may consist of strings, bytes, numbers, tuples, lists, dictionaries, sets, booleans and None.

Alternatively, you can use the json.loads() method.

Use the json.loads() method to convert the string representation of a dictionary to a dictionary, e.g. my_dict = json.loads(my_str). The json.loads() method takes a valid JSON string and parses it into a native Python object.

main.py
import json my_str = '{"id": 0, "name": "Alice", "salary": 100}' my_dict = json.loads(my_str) print(my_dict) # 👉️ {'id': 0, 'name': 'Alice', 'salary': 100} print(type(my_dict)) # 👉️ <class 'dict'>

We used the json.loads() method to parse the string representation of a dictionary to a native dict object.

Note that the json.loads() method can only be used if you have a valid JSON string.

For example, the keys and values have to be double-quoted. This wouldn't work if the keys or values are wrapped in single quotes.

The json.loads method parses a JSON string into a native Python object.

main.py
import json json_str = '{"name": "Alice", "age": 30}' my_dict = json.loads(json_str) print(type(my_dict)) # 👉️ <class 'dict'>

If the data being parsed is not a valid JSON string, a JSONDecodeError is raised.

If your string isn't valid JSON, you can use the PyYAML module.

First, install the module by running the pip install pyyaml command.

shell
pip install pyyaml

Now you can import the module and use it to parse the string into a native Python dictionary.

main.py
import yaml my_str = "{'id': 0, 'name': 'Alice', 'salary': 100}" my_dict = yaml.full_load(my_str) print(my_dict) # 👉️ {'id': 0, 'name': 'Alice', 'salary': 100} print(type(my_dict)) # 👉️ <class 'dict'>
The yaml.full_load() method takes a YAML document, parses it and produces the corresponding Python object.

Note that using the full_load() method with untrusted input is not recommended.

If you're working with untrusted data, use the yaml.safe_load() method instead.

main.py
import yaml my_str = "{'id': 0, 'name': 'Alice', 'salary': 100}" my_dict = yaml.safe_load(my_str) print(my_dict) # 👉️ {'id': 0, 'name': 'Alice', 'salary': 100} print(type(my_dict)) # 👉️ <class 'dict'>

The yaml.safe_load() method loads a subset of the YAML language. This is recommended for loading untrusted input.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.