0

I have a JSON file. I would like to convert this JSON file into CSV. I have gone through several posts in the Stack Overflow and R Studio Community. After reading them, I wrote a code.

My code is working only for the first row. This means, I am getting only 1st row from the JSON file. But, my JSON file containing 1024 rows. I check the file through the online website and received 1024 rows.

Compound <- fromJSON(file = "~/Compound.json")
df <- data.frame(t(sapply(Compound,c)))

Could you tell me, why I am getting only 1 row or how can I solve these issues?

0Knowledge
  • 747
  • 3
  • 14
  • Please share example data so we can reproduce your error. – Taufi Feb 01 '21 at 15:14
  • If your file has one complete JSON object per line/row, then it is technically "ndjson" (newline-delimited json), which is parsed correctly by `jsonlite::stream_in`. – r2evans Feb 01 '21 at 15:17
  • @r2evans thanks. Maybe this is the problem. Could you tell me how can I solve the problem? – 0Knowledge Feb 01 '21 at 15:21
  • 1
    *I did.* If your data is truly ndjson, replace your call to `fromJSON` with `stream_in`. If your data is not ndjson, then it won't work. I don't really know, though, since I don't know what's in the file. – r2evans Feb 01 '21 at 15:28

1 Answers1

1

I solved the problem using jsonlite lite package. Because, my JASON was "ndjson" (newline-delimited json). Received help from a post comment by r2evans

install.packages("jsonlite")
library(jsonlite)

json <- stream_in(file("~/Food.json"), simplifyDataFrame=FALSE)
df <- data.frame(t(sapply(json,c)))
0Knowledge
  • 747
  • 3
  • 14