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 json package in Python script. The text in JSON is done through quoted-string which contains value in key-value mapping within { }. It is similar to the dictionary in Python.
Function Used:
-
json.load[]: json.loads[] function is present in python built-in json module. This function is used to parse the JSON string.
Syntax: json.load[file_name]
Parameter: It takes JSON file as the parameter.
Return type: It returns the python dictionary object.
Example 1: Lets suppose the JSON file looks like this:
We want to convert the content of this file to Python dictionary. Below is the implementation.
Output :
Example 2: Reading nested data
In the above JSON file, there is a nested dictionary in the first key people1. Below is the implementation of reading nested data.
Output :
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning Basic Level Course