Convert JSON String to Dictionary Python
In this example, we are going to convert a JSON string to Python Dictionary using json.loads() method of JSON module in Python. Firstly, we import json module and then define JSON string after that converting JSON string to Python dictionary by passing it to json.loads() in parameter. We have print the dictionary and their values using the keys as seen in the output.
Python3
# Import JSON module import json # Define JSON string jsonString = '{ "id": 121, "name": "Naveen", "course": "MERN Stack"}' # Convert JSON String to Python student_details = json.loads(jsonString) # Print Dictionary print (student_details) # Print values using keys print (student_details[ 'name' ]) print (student_details[ 'course' ]) |
{'id': 121, 'name': 'Naveen', 'course': 'MERN Stack'} Naveen MERN Stack
Convert JSON to dictionary in Python
JSON stands for JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the Python JSON package into Python script. The text in JSON is done through quoted-string which contains a value in key-value mapping within { }. It is similar to the dictionary in Python.
Function Used
json.load(): json.load() function is present in Python built-in āJSONā module. This function is used to parse the JSON string.
json.loads(): json.loads() function is present in Python built-in ājsonā module. This function is used to parse the JSON string.
Contact Us