0% found this document useful (0 votes)
8 views18 pages

Lecture 6

It is python file

Uploaded by

r8889127
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views18 pages

Lecture 6

It is python file

Uploaded by

r8889127
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

JSON(JavaScript

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]"} ]}

JSON syntax is derived from JavaScript object notation syntax:


• Data is in name/value pairs
• Data is separated by commas
• Curly braces hold objects
• Square brackets hold arrays
Characteristics of JSON
• Human-readable and writable: JSON is easy to read and write.
• Lightweight text-based data interchange format: JSON is simpler to read and
write when compared to XML.
• Widely used: JSON is a common format for data storage and communication on
the web.
• Language-independent: Although derived from JavaScript, JSON can be used with
many programming languages.
JSON Data types
Data Type Description Example
String A string is always written in "student", "name",
double-quotes. It may consist of "1234", "Ver_1"
numbers, alphanumeric and
special characters.
Number Number represents the numeric 121, 899
characters.
Boolean It can be either True or False. true
Null It is an empty value.
JSON object
• JSON objects refer to dictionaries, which are enclosed in curly braces, i.e., { }.
• A JSON object is a collection of key/value pairs. The keys are strings, and the
values can be strings, numbers, objects, arrays, true, false, or null.

{"name" : "Jack", "employeeid" : 001, "present" : false}

{
"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

# Sample JSON data in "data.json"


# {"name": "John Doe", "age": 30, "city": "New York"}
file_path = "data.json"

# Reading JSON data from the file


with open(file_path, "r") as json_file:
data = json.load(json_file)

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"
}

# Writing JSON data to a file


file_path = "output.json"
with open(file_path, "w") as json_file:
json.dump(data, json_file)

# The "output.json" file will contain: {"name": "John


Doe", "age": 30, "city": "New York"}
Writing JSON file
Writing JSON file
Loading JSON Data as Python Objects:
import json
# Sample JSON data in "nested_data.json"
# {"person": {"name": "John Doe", "age": 30}, "city": "New
York"}
file_path = "nested_data.json"
# Reading JSON data from the file
with open(file_path, "r") as json_file:
data = json.load(json_file)

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

You might also like