Reputation: 416
I have a data frame on trending YouTube videos that has an integer column category_id
representing the ID of the video category. This ID is stored in a seperate .json file like that looks like:
{
"items": [
{
"kind": "youtube#videoCategory",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/Xy1mB4_yLrHy_BmKmPBggty2mZQ\"",
"id": "1",
"snippet": {
"channelId": "UCBR8-60-B28hp2BmDPdntcQ",
"title": "Film & Animation",
"assignable": true
}
},
{
etc..
}
]
}
As you can see the "id"
is defined in the first level of the file and I would also need "title"
through snippet { }
.
I have imported the JSON data into R with the rjson
library as follows:
library(rjson)
json_list <- fromJSON(file = "C:\\...\\US_category_id.json")
Which returns a list object of 1. When I try to turn this into a data frame through json_df <- as.data.frame(json_list$items)
it returns a flat data frame of one row and a bunch of columns.
What is the best practice here to return a data frame on the .json file containing the "id"
and "title"
so I can combine it with my other data frame down the line?
Here's what the list object of 1 looks like in RStudio
Upvotes: 0
Views: 146
Reputation: 8844
Perhaps try:
pluck_json <- function(json, sel) {
vapply(json, `[[`, character(1L), sel)
}
result <- data.frame(
id = pluck_json(json_list$items, "id"),
title = pluck_json(json_list$items, c("snippet", "title"))
)
Upvotes: 1