22: JSON with Python
JSON with Python
JSON are simple strings but they have huge applications in computing technology. We have to work with JSON often, especially in network-based applications. JSON is also similar to dictionaries which we have already studied in the 16th lecture Collection.
- JSON (JavaScript Object Notation) is a method for transporting and storing data.
- JSON is simple string data and the file created with JSON data is a text file.
- It seems that this belongs to JavaScript which is also a programming language but JSON can handle it independently.
- JSON data can be handled by almost all programming languages.
- JSON is very useful when having to transport data from one application to another application.
- For example, we are working on the School Management system.
- This is very large software and has many modules like Admission System, Examination System, Attendance System, Fees & Dues Management System and many more.
- It is possible due to budget or technical constraints; the Admission System was developed in PHP and the Examination System was built in Python.
- Both applications are working differently but we know that both systems are required to share the data. For instance, only such students participate in exams which secure their admission.
- The admission system has the data of students while the examination system is required to manage their educational performance record.
- Now the admission system must provide a record of the examination.
- The admission system is built with PHP and itself is a complex software and we do want to change it to integrate it with examination.
- We just have to add a small code that generates a JSON file like “students.json”.
- This file contains the admitted student’s data in JSON format like this.
Figure 1: Structure of a JSON File
- We can see that the JSON file contains records of 3 students and this file can save records of thousands of students.
- When data is saved to a JSON file, now this file can easily process it.
- If we analyze the data, we can see that this is a list of dictionaries in python.
- We will open this file like a simple text but should convert the string into a Python variable.
- To manipulate JSON data, we use the json library.
import jsonfile = open ("studentdata.json", "r")students = json.load (file)file.close()
Code 1: Opening JSON File
- First, we import json library
- Open the json file as a standard text file and we get a file reference.
- Library function load returns the list by converting file reference.
- Now we can traverse the list by for or while loop as lists are indexed.
for student in students:Â Â print(f"Name: {student['name']}")Â Â print(f"Father Name: {student['fatherName']}")Â Â print(f"Age: {student['age']}")Â Â print(f"Class: {student['classAdmission']}")Â Â print ("--------------------")
Code 2: Getting Data of each Student and Print
- Now we apply a loop on students variable and print the data of individual student record.
Figure 2: Output of Code 2
- Now we can put student examination data in it like we are adding marks key in each student dictionary. A simple change inside for loop makes it possible.
for student in students:  print(f"Name: {student['name']}")  print(f"Father Name: {student['fatherName']}")  print(f"Age: {student['age']}")  print(f"Class: {student['classAdmission']}")    marks = int (input("Enter marks: "))  student['marks'] = marks  print ("--------------------")
Code 3: Inserting Marking Record for each Student
- As our list is now updated so we can store it instudentGrade.json.
- We can store data into text format but it can cause some casting errors because some values in this data are not string.
file = open("exam_record.json", "w")json.dump(students, file)file.close()
Code 4: Storing JSON File
- Open the file in write mode. It will return file reference.
- Using dump function in json library, first, we provide our list (students) then file reference.
- A new file will create with name of exam_record.json
Code 5: Output of Updated JSON
- We can see that marks attributed add to each student record.








