How to convert JSON to dictionary in Python?
JSON is a vastly used human-readable and machine-friendly data format that facilitates cross-platform data exchange in various languages. On the other hand, a dictionary is a predominantly used data structure in Python. We often convert JSON data to a Python dictionary to seamlessly integrate with Python ecosystem libraries and store data in the database.
JSON format
The data in JSON format is stored inside the curly brackets. It consists of a key-value pair, each separated by a comma. The components are enclosed in double quotes and separated by a colon. The keys can be repeated; however, it is not recommended to use duplicate keys.
Dictionary format
The data in a Python dictionary is stored in the curly brackets and assigned to a defined dictionary variable. It consists of a key-value pair, each separated by a comma. The components are enclosed in single quotes and separated by a colon. The keys must be unique and immutable so that the data can be accessed and manipulated through them.
Converting JSON to a dictionary
The json module in Python offers functionalities related to encoding and decoding data in JSON. Hence, to convert a JSON string to a dictionary, we use the loads function from this module.
Required Import
import json
Syntax
json.loads(jsonString)
Parameters
This method requires only one necessary parameter:
jsonString: It is the JSON format string to be converted to a Python dictionary.
Optional parameters for the method:
parse_float: To specify how the floating point value should be parsed from JSON to dictionary.parse_int: To specify how the integer value should be parsed from JSON to dictionary.encoding: To specify the type of encoding of the JSON string e.g. 'utf-8'.cls: To specify the JSON decoder class to use for parsing.
Code example
In this example, we convert a JSON string into a Python dictionary and print their datatypes to see the difference.
import jsonuserJSON = '{"fname" : "Taylor" , "lname" : "Swift"}'print(type(userJSON))print('\nConverting JSON to dictionary:\n')#json to dictionaryuser = json.loads(userJSON)print(user)print(type(user))print('\nFirst Name : ' + user['fname'] + '\n')
Code explanation
Line 1: Import the
jsonmodule in Python.Line 3: Assign a JSON string to
userJSON. The string containsfnameandlnamealong with the values.Line 4: Print the type of the
userJSONstring. It will show<class 'str'>.Line 8: Assign a dictionary object to
uservariable by sendinguserJSONas a parameter to thejson.loads()conversion method.Line 10: Our print
userdictionary and notice that all the values are printed corresponding to their keys.Line 11: Print the type of the
userobject. It will show<class 'dict'>which shows that the conversion of successful.Line 12: We can apply simple dictionary operations to the
userobject. In this case, we print the value against thefnamekey.
Note: We can also convert Python dictionaries to JSON strings.
Test your understanding
str = '{"price": 5.54, "qty": 5}'
data = json.loads(str, parse_float=float)
print(data["price"] * data["quantity"])
What will be the output for this?
Free Resources