Converting JSON to Excel using R

2.5k Views Asked by At

I'm new to R, and I'm practicing converting JSon to R, then to CSV or Excel. I downloaded the entire Magic the Gathering card set from: https://mtgjson.com/ to practice. I used this code to get it into a "list", but I'd like it in a dataframe or table.

   json_file<- rjson::fromJSON(file= "C://Users/ahalman/Desktop/AllCards.json")

I tried:

   as.data.frame(json_file)

but I get an error message that says: "Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 13, 10, 14, 15, 11, 12, 16, 19, 9, 8, 6, 7, 18, 4, 17, 21, 23, 20, 22"

I thought this would fix it, as I saw it on another Stack Overflow page, but whatever it does, it didn't work:

  json_file <- lapply(json_file, function(x) {
  x[sapply(x, is.null)] <- NA
  unlist(x)
  })

Any help would be great. Once it's in a dataframe format, I got this!

1

There are 1 best solutions below

0
Alex On

I believe this is the answer:

  df<- stringi::stri_list2matrix(json_file, byrow = TRUE)

This works for the first few columns, but things start to get jumbled. No idea why.

I think I finally figure it out:

 newdf<- stringi::stri_list2matrix(json_file, byrow = TRUE, fill = "")