Created
January 3, 2024 20:24
-
-
Save mermelstein/397859121ad55dfeb7d300f6e0428115 to your computer and use it in GitHub Desktop.
convert Spotify json data to a dataframe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(jsonlite) | |
library(lubridate) | |
# specify local path to the downloaded JSON files | |
path <- "~/Downloads/Spotify Extended Streaming History/" | |
# get a list of all JSON files in the directory | |
json_files <- list.files(path, pattern = "*.json", full.names = TRUE) | |
# initialize an empty list to store the data | |
data_list <- list() | |
# loop through each file | |
for (i in seq_along(json_files)) { | |
# read the json file into a dataframe | |
data <- fromJSON(json_files[i]) | |
# append the new dataframe to the list | |
data_list[[i]] <- data | |
} | |
# combine all the data frames in the list into one final data frame | |
data_combined <- do.call(rbind, data_list) | |
# convert 'ts' to a date format | |
data_combined$ts <- as.Date(data_combined$ts) | |
# eg filter for June 2022 | |
data_filtered <- data_combined[year(data_combined$ts) == 2022 & month(data_combined$ts) == 6, ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment