JSON is an abbreviation for JavaScript Object Notation. It means that data is stored and transferred using a script (executable) file comprised of text in a computer language. Python has a built-in library named json that handles JSON. In order to use this feature, we must first load the json package into our Python script. The text in JSON is represented by a quoted-string, which contains the value in the key-value mapping within flower braces { }.
How to Read from JSON?
In Python, loading a JSON object is simple. Python includes a library named json that may be used to work with JSON data. It is accomplished by utilizing the JSON module, which gives us a plethora of methods, among which the loads() and load() methods will assist us in reading the JSON file.
JSON deserialization:
Deserialization of JSON refers to the process of converting JSON objects into their corresponding Python objects. It is accomplished through the usage of the load()/loads() method. If you used JSON data from another program or received it in a JSON string format, it may be simply deserialized with load()/loads(), which is typically used to load from strings; otherwise, the root object is in list or dict. See the table below for further information.
JSON Object | Python Object |
object | dict |
array | list |
string | str |
null | None |
number(int) | int |
number(real) | float |
true | True |
false | False |
You may want to quickly read and parse json data from relevant JSON files always. Here we look at some of the methods we may use to easily read and extract this data.
- How To Convert JSON To CSV File in Python?
- How to Convert CSV to JSON Array in Python?
- Python Serialize and Deserialize JSON to Objects
1)Reading a JSON File using load() Function:
The json module is a Python3 built-in module that provides JSON file handling capabilities using json.load ()
Using this approach, we may create a Python object after directly reading a JSON file in Python.
Here we take demo.json as an Example
demo_data{ "Article By": "Btechgeeks", "topic": "JSON", "language": "Python" }
Using the program below, we can import the Json objects into a Python object. We can now simply access it by utilizing dictionary key: value pair mappings.
Example
Approach:
- Import json module using the import Keyword
- Open the json file using the open() method by passing the json file name
as an argument to it - Pass the above json file to the load() function to get the above JSON object as
a dictionary. - Iterate in the above-declared json objects using the for loop
- Inside the loop print the value of the json object.
- Close the json file using the close() function.
- The Exit of the Program.
Below is the implementation:
# Import json module using the import Keyword import json # Open the json file using the open() method by passing the json file name # as an argument to it json_file = open('student.json') # Pass the above json file to the load() function to get the above JSON object as # a dictionary rslt_data = json.load(json_file) # Iterate in the above declared json objects using the for loop for itr in rslt_data['student_info']: # Inside the loop print the value of the json object. print(itr) # Close the json file using the close() function json_file.close()
Output:
{'roll_no': 1, 'name': 'john', 'section': 'A'} {'roll_no': 2, 'name': 'peter', 'section': 'B'} {'roll_no': 3, 'name': 'alex', 'section': 'C'}
File Content(student.json):
{ "student_info": [ { "roll_no": 1, "name": "john", "section": "A" }, { "roll_no": 2, "name": "peter", "section": "B" }, { "roll_no": 3, "name": "alex", "section": "C" } ] }
2)Using ijson for huge JSON Files
If your JSON file is huge enough that bringing the entire information to memory is too expensive, a preferable option would be to convert the file content into streams using ijson.
A stream is a collection of objects (similar to JSON objects) that are loaded into memory only when needed or demanded. This means that our data loader is loading data “lazily,” that is, only when it is required.
When working with large files, this reduces the amount of RAM required. The stream’s content is saved in a temporary buffer, allowing you to work with gigabytes of JSON files!
Installation:
pip install ijson
Here we take an example of student.json file
Example
# Import ijson module using the import keyword import ijson #Get all the attributes of json file using parse function and for loop for prefix, type_of_object, value in ijson.parse(open("student.json")): print(prefix, type_of_object, value)
Output:
start_map None map_key student_info student_info start_array None student_info.item start_map None student_info.item map_key roll_no student_info.item.roll_no number 1 student_info.item map_key name student_info.item.name string john student_info.item map_key section student_info.item.section string A student_info.item end_map None student_info.item start_map None student_info.item map_key roll_no student_info.item.roll_no number 2 student_info.item map_key name student_info.item.name string peter student_info.item map_key section student_info.item.section string B student_info.item end_map None student_info.item start_map None student_info.item map_key roll_no student_info.item.roll_no number 3 student_info.item map_key name student_info.item.name string alex student_info.item map_key section student_info.item.section string C student_info.item end_map None student_info end_array None end_map None
This will print the contents of the large JSON file, but you may prevent printing the entire file by keeping a counter variable.
While ijson is slow, it appears to have a lower memory bound. If you work with huge files, you should try this module.