Python dictionary is a robust data structure that deals with key-value pairs. However, when exchanging the data between the client and server, json string is used.
JSON is a standard for data exchange, and when data is available on the client side, we need to convert the json string to a dictionary.
How to convert string to dict in Python
To convert a Python string to a dictionary, use the json.loads() function. The json.loads() is a built-in function that converts a valid string to a dict.
The json.loads() function takes a json string as an argument and returns the dictionary.
str = '{"Name": "Millie", "Age": 18, "City": "Atlanata"}' print('The JSON String is', str)
Output
The JSON String is {"Name": "Millie", "Age": 18, "City": "Atlanata"}
Let’s use the json.loads() method to convert a string into a dictionary.
Before you use the loads() method, import json package into your program.
import json
Now, you can use the loads() method.
# app.py import json str = '{"Name": "Millie", "Age": 18, "City": "Atlanata"}' print('The JSON String is', str) convertedDict = json.loads(str) print("After conversion: ", convertedDict) print(type(convertedDict))
Output
The JSON String is {"Name": "Millie", "Age": 18, "City": "Atlanata"} After conversion: {'Name': 'Millie', 'Age': 18, 'City': 'Atlanata'} <class 'dict'>
You can see that the loads() function returns a dictionary, and we verified that using the type() method.
To convert a dict to string, use the json.dumps() method.
Using ast.literal_eval()
The ast.literal_eval() is a built-in Python library function that converts a string to a dict.
To use the literal_eval() function, import the ast package and use its literal_eval() method.
# app.py import ast str = '{"Name": "Millie", "Age": 18, "City": "Atlanata"}' print('The JSON String is', str) convertedDict = ast.literal_eval(str) print("After conversion: ", convertedDict) print(type(convertedDict))
Output
The JSON String is {"Name": "Millie", "Age": 18, "City": "Atlanata"} After conversion: {'Name': 'Millie', 'Age': 18, 'City': 'Atlanata'} <class 'dict'>
You can see that the output is the same as the loads() function, and it does the same thing.
Using generator expressions in Python
If we have enough strings to form a dictionary, use the generator expressions to convert the string to a dictionary.
# app.py str = "E - 11, K - 19, L - 21" convertedDict = dict((x.strip(), int(y.strip())) for x, y in (element.split('-') for element in str.split(', '))) print("Converted dict is: ", convertedDict) print(type(convertedDict))
Output
Converted dict is: {'E': '11', 'K': '19', 'L': '21'} <class 'dict'>
In this example, we have used many Python functions like dict(), strip(), int(), and split().
First, we split the string inside the for loop and converted them into a dictionary using the dict() method.
Next, the strip() method removes whitespace from the string and uses the int() function to convert the string to int for integer values.
Conclusion
The easiest way is to use the json.loads() function to convert a valid string to a dict.