Python - Ways to convert string to json object

PythonServer Side ProgrammingProgramming

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.

Example

 Live Demo

# converting string to json
# using json.loads
import json
# inititialising json object
ini_string = {'vishesh': 1, 'ram' : 5, 'prashant' : 10, 'vishal' : 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', '{"vishal": 15, "ram": 5, "vishesh": 1, "prashant": 10}')
('type of ini_object', <type 'str'>)
('final dictionary', "{u'vishal': 15, u'ram': 5, u'vishesh': 1, u'prashant': 10}")
('type of final_dictionary', <type 'dict'>)
raja
Published on 06-Aug-2020 13:46:29
Advertisements