How To Convert String Representation Of Dict To Dict In Python?

Converting string representation of dict to dict in Python is a common step in developing your project. String data is easily conveyed through methods, so data type models are often converted back and forth to facilitate communication with each other. This article will help you complete it.

Two ways to convert string representation of dict to dict in Python

Using json.loads() method

As we introduced above, converting data to string aims to transmit it more gently and quickly. So Python will support methods to converting string representation of dict to dict easily, specifically here is using the loads() method from the json library.

Syntax

json.loads(param)

Parameter

  • param: is a json string

The result returns a Python object.

Specific examples of the requirements of the problem:

import json
 
# Init json string
jsonStr = '{"cow" : 1, "dogs" : 2, "cats" : 3}'
 
# Using json.loads() to convert
dictObject = json.loads(jsonStr)

# Print json string
print("The original string : " + str(jsonStr))
 
# Print dict object
print("The converted dictionary : " + str(dictObject))

# Check data type
print(type(jsonStr))
print(type(dictObject))

Output

The original string : {"cow" : 1, "dogs" : 2, "cats" : 3}
The converted dictionary : {'cow': 1, 'dogs': 2, 'cats': 3}
<class 'str'>
<class 'dict'>

First, we import the json library, which contains json related methods and loads() method.

Next, we initialize a json string with the internal format of a dict. we use the json.loads() methods to unformat the json string, and as a result, we get a dict object like the one we showed above.

Methods are so simple as to give perfect results.

Using eval() function

This is a built-in function in Python, and it has the same task as the json.loads() method. However, it is a function developed by Python to be used independently without any other library.

Syntax

eval(param)

Parameter

  • param: is a string to convert

It will return a Python expression from a string, distinguishing it from the json.loads() method is that it can recognize any expression in Python, not necessarily a data type as a json string.

Example

# Init json string
jsonStr = '{"cow" : 1, "dogs" : 2, "cats" : 3}'
 
# Using eval() function to convert
dictObject = eval(jsonStr)

# Print json string
print("The original string : " + str(jsonStr))
 
# Print dict object
print("The converted dictionary : " + str(dictObject))

# Check data type
print(type(jsonStr))
print(type(dictObject))

Output

The original string : {"cow" : 1, "dogs" : 2, "cats" : 3}
The converted dictionary : {'cow': 1, 'dogs': 2, 'cats': 3}
<class 'str'>
<class 'dict'>

we replaced json.loads() method with eval() function and it gives the same result

Summary

Regardless of json.loads() method or eval() function gives the same result in converting string representation of dict to dict in Python. It may differ in other cases, which you will learn in upcoming articles. Thank you for reading the article. Good luck to you.

Leave a Reply

Your email address will not be published. Required fields are marked *