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
# 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)) |
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()
# 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)) |
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’>
Recommended Posts:
- Convert Json String to Java Object Using GSON
- Convert Java Object to Json String using GSON
- Convert Java Object to Json String using Jackson API
- Python | Convert dictionary object into string
- JSON | modify an array value of a JSON object
- Python | Check whether a string is valid json or not
- JavaScript | Convert an array to JSON
- JavaScript | How to add an element to a JSON object?
- JavaScript | Add new attribute to JSON object
- Converting JSON text to JavaScript Object
- Python | Ways to convert array of strings to array of floats
- Reverse string in Python (5 different ways)
- Find length of a string in python (4 ways)
- Ways to remove i'th character from string in Python
- Python | Convert String to bytes
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.



