Statistical Analysis of JSON Data

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to perform statistical analysis on JSON data using Python. You will parse a JSON file containing user study data, extract specific information, and calculate the number of courses studied and the total number of minutes studied for a given user.

👀 Preview

$ python3 analysis.py
(4, 12)

🎯 Tasks

In this project, you will learn:

  • How to read and parse a JSON file using Python
  • How to extract specific data items from the JSON data
  • How to perform statistical analysis on the extracted data

🏆 Achievements

After completing this project, you will be able to:

  • Develop a Python function to analyze JSON data
  • Calculate the number of courses studied and the total minutes studied for a given user
  • Apply basic data analysis techniques to extract insights from structured data

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/FileHandlingGroup -.-> python/file_opening_closing("`Opening and Closing Files`") python/PythonStandardLibraryGroup -.-> python/data_serialization("`Data Serialization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/with_statement -.-> lab-302770{{"`Statistical Analysis of JSON Data`"}} python/conditional_statements -.-> lab-302770{{"`Statistical Analysis of JSON Data`"}} python/for_loops -.-> lab-302770{{"`Statistical Analysis of JSON Data`"}} python/lists -.-> lab-302770{{"`Statistical Analysis of JSON Data`"}} python/tuples -.-> lab-302770{{"`Statistical Analysis of JSON Data`"}} python/function_definition -.-> lab-302770{{"`Statistical Analysis of JSON Data`"}} python/importing_modules -.-> lab-302770{{"`Statistical Analysis of JSON Data`"}} python/standard_libraries -.-> lab-302770{{"`Statistical Analysis of JSON Data`"}} python/file_opening_closing -.-> lab-302770{{"`Statistical Analysis of JSON Data`"}} python/data_serialization -.-> lab-302770{{"`Statistical Analysis of JSON Data`"}} python/build_in_functions -.-> lab-302770{{"`Statistical Analysis of JSON Data`"}} end

Implement the analysis Function

In this step, you will implement the analysis function to parse the JSON file and analyze the user data.

  1. Add the following code to the analysis.py file:
import json


def analysis(file, user_id):
    """
    Load json into this function and parse the data
    """
    with open(file, "r", encoding="utf-8") as f:
        data = json.load(f)

    courses_studied = 0
    total_minutes = 0

    for item in data:
        if item["user_id"] == user_id:
            courses_studied += 1
            total_minutes += item["minutes"]

    return courses_studied, total_minutes
  1. The analysis function takes two parameters:

    • file: the name of the JSON file containing the user study data
    • user_id: the ID of the user whose data you want to analyze
  2. The function reads the JSON file, iterates through the data, and counts the number of courses studied and the total number of minutes studied for the specified user.

  3. The function returns two values:

    • courses_studied: the number of courses studied by the specified user
    • total_minutes: the total number of minutes studied by the specified user

Test the analysis Function

In this step, you will test the analysis function by calling it with the provided user_study.json file and a user ID.

  1. Add the following code at the end of the analysis.py file:
if __name__ == "__main__":
    print(analysis("user_study.json", 131866))
  1. Save the analysis.py file.
  2. Run the analysis.py script from the terminal:
python3 analysis.py
  1. The output should be a tuple containing the number of courses studied and the total number of minutes studied for the user with ID 131866.
(4, 12)

Congratulations! You have completed the project. You can now use the analysis function to analyze the user study data in the user_study.json file.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like