Search code examples
rjsoncsvrjson

JSON to CSV (getting only first row from the JSON file)


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?


Solution

  • 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)))