0

I'm trying but not able to understand how to convert the below json:

["{\"tables\":[{\"name\":\"PrimaryResult\",\"columns\":[{\"name\":\"timestamp\",\"type\":\"datetime\"},{\"name\":\"count_\",\"type\":\"long\"}],\"rows\":[[\"2020-10-26T00:00:00Z\",13937],[\"2020-10-25T00:00:00Z\",94029]]}]}"] 

into a data frame like this one:

enter image description here

Phil
  • 7,287
  • 3
  • 36
  • 66
esmemas
  • 1
  • 1
  • Have you searched within stackoverflow? See this link. It might work for you by using jsonlite package: https://stackoverflow.com/questions/36454638/how-can-i-convert-json-to-data-frame-in-r – sreedta Oct 26 '20 at 14:21
  • Have you checked your `json` string. It shows invalid data when parsed on `firefox`. Please check. – ambrish dhaka Oct 26 '20 at 15:30

1 Answers1

0

Using jsonlite::fromJSON.

json <- "{\"tables\":[{\"name\":\"PrimaryResult\",\"columns\":[{\"name\":\"timestamp\",\"type\":\"datetime\"},{\"name\":\"count_\",\"type\":\"long\"}],\"rows\":[[\"2020-10-26T00:00:00Z\",13937],[\"2020-10-25T00:00:00Z\",94029]]}]}"
dat <- jsonlite::fromJSON(json)
res <- setNames(as.data.frame(dat$tables$rows[[1]]), unlist(dat$tables$columns[[1]][1, ]))
res
#              timestamp datetime
# 1 2020-10-26T00:00:00Z    13937
# 2 2020-10-25T00:00:00Z    94029
jay.sf
  • 60,139
  • 8
  • 53
  • 110