Uncategorized

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.

  1. JSON (JavaScript Object Notation) is a method for transporting and storing data.
  2. JSON is simple string data and the file created with JSON data is a text file.
  3. It seems that this belongs to JavaScript which is also a programming language but JSON can handle it independently.
  4. JSON data can be handled by almost all programming languages.
  5. JSON is very useful when having to transport data from one application to another application.
  6. For example, we are working on the School Management system.
  7. This is very large software and has many modules like Admission System, Examination System, Attendance System, Fees & Dues Management System and many more.
  8. It is possible due to budget or technical constraints; the Admission System was developed in PHP and the Examination System was built in Python.
  9. 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.
  10. The admission system has the data of students while the examination system is required to manage their educational performance record.
  11. Now the admission system must provide a record of the examination.
  12. 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.
  13. We just have to add a small code that generates a JSON file like “students.json”.
  14. This file contains the admitted student’s data in JSON format like this.

Figure 1: Structure of a JSON File

  1. We can see that the JSON file contains records of 3 students and this file can save records of thousands of students.
  2. When data is saved to a JSON file, now this file can easily process it.
  3. If we analyze the data, we can see that this is a list of dictionaries in python.
  4. We will open this file like a simple text but should convert the string into a Python variable.
  5. 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

  1. First, we import json library
  2. Open the json file as a standard text file and we get a file reference.
  3. Library function load returns the list by converting file reference.
  4. 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

  1. Now we apply a loop on students variable and print the data of individual student record.

Figure 2: Output of Code 2

  1. 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

  1. As our list is now updated so we can store it instudentGrade.json.
  2. 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

  1. Open the file in write mode. It will return file reference.
  2. Using dump function in json library, first, we provide our list (students) then file reference.
  3. A new file will create with name of exam_record.json

Code 5: Output of Updated JSON

  1. We can see that marks attributed add to each student record.

Leave a Reply

Your email address will not be published. Required fields are marked *