How to convert a list with same type of field to a data.frame in R How to convert a list with same type of field to a data.frame in R json json

How to convert a list with same type of field to a data.frame in R


You can use jsonlite.

library(jsonlite)

Then use fromJSON() and specify the path to your file (or alternatively a URL or the raw text) in the argument txt:

fromJSON(txt = 'path/to/json/file.json')

The result is:

     floors elevation     bmi1         5        15 23.74832         4        12 23.76403         3         9 23.7797

If you prefer rjson, you could first read it as previously:

data <- rjson::fromJSON(file = 'path/to/json/file.json')

Then use do.call() and rbind.data.frame() to convert the list to a dataframe:

do.call("rbind.data.frame", data)

Alternatively to do.call(): use data.tables rbindlist() which is faster:

data.table::rbindlist(data)