Lecture 6
Lecture 6
Object Notation)
File Handling
JSON file structure
• JSON stands for JavaScript Object Notation.
• JSON is lightweight data-interchange format.
• JSON is language independent.
• JSON supports array, object, string, number and values.
• Web applications commonly use JSON to exchange data between each other.
{"employees":[
{"name":"Sunny", "email":”[email protected]"},
{"name":"Rahul", "email":"[email protected]"},
{"name":"John", "email":"[email protected]"} ]}
{
"employee": {
"name": "sonoo",
"salary": 56000,
"married": true
}
}
JSON Array
• A JSON array is an ordered collection of values. The values can be strings, numbers,
objects, arrays, true, false, or null.
[
{
"PizzaName" : "Country Feast",
"Base" : "Cheese burst",
"Toppings" : ["Jalepenos", "Black Olives", ", "Cherry tomatoes"],
"Spicy" : "yes", },
{
"PizzaName" : "Veggie Paradise",
"Base" : "Thin crust",
"Toppings" : ["Jalepenos", "Black Olives", "Cherry tomatoes"],
"Spicy" : "yes",
}
]
Reading JSON File
import json
print(data)
# Output: {'name': 'John Doe', 'age': 30, 'city': 'New
York'}
Reading JSON File
Writing JSON file
import json
# Sample Python dictionary
data = {
"name": "John Doe",
"age": 30,
"city": "New York"
}
print(data)
# Output: {'person': {'name': 'John Doe', 'age': 30}, 'city':
'New York'}
print(data['person']['name'])
# Output: 'John Doe'
Problem Statement
You are working as a data scientist for a healthcare organization, and your
team has been tasked with analysing COVID-19 data from multiple
countries. The data is stored in JSON files, with each file representing the
daily COVID-19 statistics for a specific country. Each JSON file has the
following structure:
{ "country": "Country Name",
"date": "YYYY-MM-DD",
"confirmed_cases": { "total": 1000, "new": 50 },
"deaths": { "total": 20, "new": 2 },
"recovered": { "total": 800, "new": 30 }
}
Your task is to write a Python program that performs the following
operations:
1.Read COVID-19 data from all JSON files in a given directory
and its subdirectories.
2.Calculate and display the following statistics for each country:
1. Total confirmed cases.
2. Total deaths.
3. Total recovered cases.
4. Total active cases (total confirmed cases minus total deaths and total
recovered).
3.Determine the top 5 countries with the highest number of
confirmed cases and the lowest number of confirmed cases.
4.Generate a summary report in JSON format that includes the
statistics for all countries and save it to a file named
"covid19_summary.json".