Python JSON Project

Python Program to Query JSON file

Python JSON Practice Project

The JSON file “cars.json” (see below) contains data about the inventory of a used cars dealer. Your task in this Python JSON practice project is to create a program to query the JSON file to answer questions about the cars.

Write a python program to query the JSON file, use the Pandas library to read the JSON. The program will ask the user to select two possible options:

  1. Newest cars: If the user select this option, the program must display the newest car by date and the car with the minimum mileage.
  2. Type of car: If the user select this option, the program must display the count of the distinct types of cars (hybrid, diesel, etc) available for sale.

JSON (JavaScript Object Notation) files are widely used in real data projects due to their versatility, readability, and ease of use. They provide a standardized and human-readable format for storing and exchanging data. In addition, they enable seamless integration between different systems and technologies, and their flexibility makes them suitable for a wide range of use cases, from web development to big data applications. Finally, JSON’s lightweight nature and support for nested structures make it an excellent choice for representing complex data in a structured manner.

Understanding how to read, manipulate, and generate JSON data is essential for interacting with web APIs, handling configuration files, integrating with NoSQL databases, and processing log data. Moreover, JSON proficiency demonstrates a strong understanding of data serialization and deserialization, an important skill for building scalable and efficient systems. As big companies heavily rely on APIs, web services, and data exchange between systems, the ability to work with JSON is a fundamental requirement for IT roles, making it a valuable skill for Python developers in the industry.

SOURCE CODE

import pandas as pd

# Read the car data from the JSON file using Pandas
car_data = pd.read_json('cars.json')

def find_newest_car_and_min_mileage(cars):
    newest_car = cars.loc[cars['year'].idxmax()]
    min_mileage_car = cars.loc[cars['mileage'].idxmin()]
    return newest_car, min_mileage_car

def count_distinct_car_types(cars):
    type_counts = cars['type'].value_counts()
    return type_counts

# Ask the user to select an option
user_option = input("Select an option: \n1) Newest cars\n2) Type of car\n")

# Process the user's choice
if user_option == "1":
    newest_car, min_mileage_car = find_newest_car_and_min_mileage(car_data)
    print(f"The newest car is {newest_car['maker']} {newest_car['model']} from {newest_car['year']} with {newest_car['mileage']} miles.")
    print(f"The car with the minimum mileage is {min_mileage_car['maker']} {min_mileage_car['model']} with {min_mileage_car['mileage']} miles.")
elif user_option == "2":
    type_counts = count_distinct_car_types(car_data)
    print("Count of distinct types of cars:")
    print(type_counts)
else:
    print("Invalid option selected.")

Explanation of the code

  1. Import the pandas library.
  2. Read car data from the JSON file using pd.read_json.
  3. Define two functions to handle the user’s options:
    • find_newest_car_and_min_mileage finds the newest car by date and the car with the minimum mileage.
    • count_distinct_car_types counts the distinct types of cars in the dataset using the value_counts method.
  4. The user is prompted to select an option.
  5. Based on the user’s selection, the program calls the appropriate function to display the requested information.

cars.json File

{
  "cars": [
    {
      "maker": "Toyota",
      "model": "Camry",
      "year": 2018,
      "color": "Silver",
      "mileage": 45000,
      "transmission": "Automatic",
      "price": 18000,
      "type": "gas"
    },
    {
      "maker": "Honda",
      "model": "Civic",
      "year": 2019,
      "color": "Red",
      "mileage": 35000,
      "transmission": "Manual",
      "price": 16000,
      "type": "gas"
    },
    {
      "maker": "Ford",
      "model": "F-150",
      "year": 2017,
      "color": "Black",
      "mileage": 60000,
      "transmission": "Automatic",
      "price": 25000,
      "type": "gas"
    },
    {
      "maker": "BMW",
      "model": "X5",
      "year": 2020,
      "color": "White",
      "mileage": 20000,
      "transmission": "Automatic",
      "price": 40000,
      "type": "diesel"
    },
    {
      "maker": "Chevrolet",
      "model": "Malibu",
      "year": 2016,
      "color": "Blue",
      "mileage": 70000,
      "transmission": "Automatic",
      "price": 15000,
      "type": "gas"
    },
    {
      "maker": "Audi",
      "model": "A4",
      "year": 2021,
      "color": "Gray",
      "mileage": 10000,
      "transmission": "Automatic",
      "price": 35000,
      "type": "diesel"
    },
    {
      "maker": "Tesla",
      "model": "Model S",
      "year": 2019,
      "color": "Black",
      "mileage": 15000,
      "transmission": "Automatic",
      "price": 60000,
      "type": "electric"
    },
    {
      "maker": "Nissan",
      "model": "Leaf",
      "year": 2018,
      "color": "Green",
      "mileage": 20000,
      "transmission": "Automatic",
      "price": 25000,
      "type": "electric"
    },
    {
      "maker": "Hyundai",
      "model": "Ioniq",
      "year": 2020,
      "color": "Silver",
      "mileage": 10000,
      "transmission": "Automatic",
      "price": 28000,
      "type": "hybrid"
    },
    {
      "maker": "Kia",
      "model": "Niro",
      "year": 2019,
      "color": "White",
      "mileage": 18000,
      "transmission": "Automatic",
      "price": 26000,
      "type": "hybrid"
    }
  ]
}

 

We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for practice
Register New Account
Shopping cart