The Wayback Machine - https://web.archive.org/web/20230315005644/https://www.geeksforgeeks.org/python-ways-to-convert-string-to-json-object/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Ways to convert string to json object

Improve Article
Save Article
  • Last Updated : 13 Mar, 2023
Improve Article
Save Article

Data send and get generally in a string of dictionary(JSON objects) forms in many web API’s to use that data to extract meaningful information we need to convert that data in dictionary form and use for further operations. Let’s see some ways to convert string to json. 
Method #1: dict object to string object using json.loads 

Python3




# Python code to demonstrate
# converting string to json
# using json.loads
import json
 
# initialising json object
ini_string = {'nikhil': 1, 'akash' : 5,
              'manjeet' : 10, 'akshat' : 15}
 
# printing initial json
ini_string = json.dumps(ini_string)
print ("initial 1st dictionary", ini_string)
print ("type of ini_object", type(ini_string))
 
# converting string to json
final_dictionary = json.loads(ini_string)
 
# printing final result
print ("final dictionary", str(final_dictionary))
print ("type of final_dictionary", type(final_dictionary))

Output:

initial 1st dictionary {'manjeet': 10, 'nikhil': 1, 'akshat': 15, 'akash': 5}
type of ini_object <type 'str'>
final dictionary {'nikhil': 1, 'manjeet': 10, 'akshat': 15, 'akash': 5}
type of final_dictionary <type 'dict'>

Method #2: str object to dict object using eval() 

Python3




# Python code to demonstrate
# converting string to json
# using eval
 
 
# initialising json object string
ini_string = """{'nikhil': 1, 'akash' : 5,
            'manjeet' : 10, 'akshat' : 15}"""
 
# printing initial json
print ("initial 1st dictionary", ini_string)
print ("type of ini_object", type(ini_string))
 
# converting string to json
final_dictionary = eval(ini_string)
 
# printing final result
print ("final dictionary", str(final_dictionary))
print ("type of final_dictionary", type(final_dictionary))

Output:

initial 1st dictionary {'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15}
type of ini_object <class 'str'>
final dictionary {'nikhil': 1, 'manjeet': 10, 'akash': 5, 'akshat': 15}
type of final_dictionary <class 'dict'>

Method #3:  Using the ast.literal_eval function

Python3




import ast
 
# initialize the string to be converted
string = '{"name": "John", "age": 30, "city": "New York"}'
 
# use the ast.literal_eval function to parse the string and create a dictionary object
dict_obj = ast.literal_eval(string)
 
# printing final result
print ("Initial string dictionary: ",string)
print ("Final dictionary: ",dict_obj)
print(type(dict_obj))
#This code is contributed by Edula Vinay Kumar Reddy

Output

Initial string dictionary:  {"name": "John", "age": 30, "city": "New York"}
Final dictionary:  {'name': 'John', 'age': 30, 'city': 'New York'}
<class 'dict'>

In the above code, we are using the ast.literal_eval function to parse a string representation of a dictionary and create a Python dictionary object. The initial string is a JSON object representation of a dictionary with three properties: “name”, “age”, and “city”. The ast.literal_eval function parses the string and creates a Python dictionary object with the same properties and values. The resulting dictionary object has a type of dict. We can then access the properties of the dictionary object using standard dictionary access notation (e.g. dict_obj[“name”]).

This demonstrates that the ast.literal_eval function was able to successfully parse the input string and create a Python dictionary object with the same properties and values as the original JSON object.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!