Read JSON Login Days per Employee

Medium
File I/O
JSON
Aggregation

Read JSON Login Days per Employee

The file datasets/json/attendance_logs.json holds one object per attendance record - an employee can have several records for the same work_date. How many different days did every employee show up?

What to do

  1. Read the JSON file from disk. Its local path is handed to you as paths['attendance_logs'].
  2. Group the rows by employee_id and count the distinct work_date values.
  3. Keep only the employees with at least three different days.
  4. Return the ten employees with the most days.
  5. Sort by work_days descending, then by employee_id ascending.
  6. Return exactly these columns: employee_id, work_days.

Signature

File questions receive the path as a third argument:

from pyspark.sql import functions as F

def solve(spark, dfs, paths):
    logs = spark.read.json(paths['attendance_logs'])
    # TODO: aggregate, filter and sort
    return logs

The same data is also preloaded as dfs['attendance_logs'] if you prefer to skip the file read.

This question runs in your browser - no sign-in needed. Press Run for the sample data and Submit for the hidden data.

Browser Engine: Evaluates PySpark DataFrame & SQL logic locally in Pyodide WebAssembly.

Input files

1 File
Loaded automatically

These files are loaded into this browser when you run. Their paths are handed to your code as the third argument: solve(spark, dfs, paths). Read one with spark.read.csv(paths['users'], header=True), or skip the read and use dfs['users'].

paths[attendance_logs]
json
2000 rows
datasets/json/attendance_logs.json
log_id(bigint)
employee_id(int)
work_date(date)
login_time(timestamp)
logout_time(timestamp)
status(string)

Return these columns

employee_id(bigint)
work_days(bigint)

Column names and order must match. Sort order is specified in the question.

Browser engine
Local files
Loading Python editor…
Click Run to try the sample cases, or Submit to check the hidden cases. First run caches the browser Python engine (~8–15s once); all code stays on your device.