Add the flatten = TRUE parameter when reading with fromJSON . This will give you a nested list in which at the deepest level a list of three data frames will be presented. Using:
library(jsonlite)
gives:
> dat time midpoint.x midpoint.y realworld.x realworld.y coordinate.x coordinate.y track 1 2015 6 8 1 3 16 38 1 2 2015 6 8 1 3 16 37 1 3 2016 6 9 2 3 16 38 1 4 2015 5 9 -1 3 16 38 2 5 2015 5 9 -1 3 16 38 2 6 2016 5 9 -1 3 16 38 2 7 2015 3 15 -9 2 17 38 2 8 2015 6 7 -2 3 16 39 3
Another, shorter approach uses jsonlite in combination with rbindlist from the data.table package:
library(jsonlite) library(data.table)
or bind_rows from the bind_rows package dplyr similar way:
library(dplyr) dat <- bind_rows(jsondata$data$track, .id = 'track')
Used data:
txt <- '{ "data":[ {"track":[ {"time":"2015","midpoint":{"x":6,"y":8},"realworld":{"x":1,"y":3},"coordinate":{"x":16,"y":38}}, {"time":"2015","midpoint":{"x":6,"y":8},"realworld":{"x":1,"y":3},"coordinate":{"x":16,"y":37}}, {"time":"2016","midpoint":{"x":6,"y":9},"realworld":{"x":2,"y":3},"coordinate":{"x":16,"y":38}} ]}, {"track":[ {"time":"2015","midpoint":{"x":5,"y":9},"realworld":{"x":-1,"y":3},"coordinate":{"x":16,"y":38}}, {"time":"2015","midpoint":{"x":5,"y":9},"realworld":{"x":-1,"y":3},"coordinate":{"x":16,"y":38}}, {"time":"2016","midpoint":{"x":5,"y":9},"realworld":{"x":-1,"y":3},"coordinate":{"x":16,"y":38}}, {"time":"2015","midpoint":{"x":3,"y":15},"realworld":{"x":-9,"y":2},"coordinate":{"x":17,"y":38}} ]}, {"track":[ {"time":"2015","midpoint":{"x":6,"y":7},"realworld":{"x":-2,"y":3},"coordinate":{"x":16,"y":39}} ]}]}'