Python | Ways to convert string to json object

Data send and get generally in 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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# converting string to json 
# using json.loads
import json
  
# inititialising 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))

chevron_right


Output:

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# converting string to json 
# using eval
  
  
# inititialising 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))

chevron_right


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’>



My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.