- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Ways to convert string to json object
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
# 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'>)

Advertisements