Search code examples
rrjson

Key-value pairs with character data to a dataframe in R?


I had been following this guide for how to convert a key-value dictionary to a dataframe in R using rjson, but I can't seem to get it to work with my data:

{"tagid":493,"name":"Early Access","count":75}
{"tagid":599,"name":"Simulation","count":68,"browseable":true}
{"tagid":1755,"name":"Space","count":64,"browseable":true}

Doesn't seem to want to parse into a dataframe because of the character values for the name key:

Error in FUN(X[[i]], ...) : unexpected character '''

I'm using the same code as the example I linked to:

library(rjson)
Lines <- readLines("clipboard") 
json_df <- as.data.frame(t(sapply(Lines, fromJSON)))

Is there a way to do a similar conversion of the dictionary data to a data frame here if there's character data?

Edit: the following script is what I'm using to generate the data:

webpage <- read_html("https://store.steampowered.com/app/387290")
data <- html_nodes(webpage, css = "script") %>% html_text()

tag_data <- data[lapply(data,function(x) length(grep("InitAppTagModal",x,value=FALSE))) == 1]
tag_data <- regmatches(tag_data, gregexpr("[?<=\\[].*?[?=\\]]", tag_data, perl=T))[[1]][1]
tag_data <- gsub('[', "", tag_data, fixed = TRUE)
tag_data <- gsub(']', "", tag_data, fixed = TRUE)
tag_data <- gsub("},{", "}\n{", tag_data, fixed = TRUE)

writeLines(tag_data, con = "temp.json", sep = "\n")
tag_df <- stream_in(file("temp.json"))

Solution

  • I put your example data into a json file. And I named it test.json

    library(jsonlite)
    
    myoutput <- stream_in(file("test.json"))
    myoutput
    
      tagid         name count browseable
    1   493 Early Access    75         NA
    2   599   Simulation    68       TRUE
    3  1755        Space    64       TRUE