0

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!

Alex
  • 77
  • 1
  • 10
  • 1
    I'm actually working with this same data. Unluckily for you, it's not a trivial thing to make it tabular. My approach was creating two tables: one for the cards, the other for the sets. It involves liberal usage of `lapply()` and `vapply()`. – Nathan Werth Jul 17 '17 at 21:06
  • @NathanWerth I think I figured it mostly out with my answer below. Check it out. Just requires a bit of cleaning. I used the "stringi" package. – Alex Jul 18 '17 at 12:55
  • Seems like my work blocks mtgjson.com, so I'll have to try it when I get home. And sorry for missing the part where you're only dealing with the cards data. – Nathan Werth Jul 18 '17 at 13:26
  • All good, so it looks like the code worked for the first few columns, then I start getting weird results. – Alex Jul 18 '17 at 16:12

1 Answers1

1

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 = "")
Alex
  • 77
  • 1
  • 10