1
votes

I have a json format time series data and I would like to extract the data from json format and convert it to data frame in R. I have installed rjson and jsonlite packages and loaded the library. However, I encountered the following error when I ran "fromjson" command. Does anyone know how to resolve it ?

Thanks

Rcode

   library("rjson", lib.loc="/Users/r_beginer/Library/R/3.0/library")
   library("rjsonlite", lib.loc="/Users/r_beginer/Library/R/3.0/library")
   test_data=fromJSON(file='/Users/r_beginer/Desktop/logins.json')
   Error in is.character(txt) : 'txt' is missing

json format data

  ["2014-03-01 00:01:54", "2014-03-01 00:04:52", "2014-03-01 00:06:03", 
   "2014-03-01 00:12:11", "2014-03-01 00:14:54", "2014-03-01 00:16:23", "2014-03-01 00:17:19"]
1
I assume json data is key:value pairs... I did not see that in your data, it is just a plain list of timestamps.B.Mr.W.
May be this link helps https://stackguides.com/questions/21121699/unable-to-convert-json-to-dataframeakrun

1 Answers

0
votes

First, I think you may have wished to call library(jsonlite) not rjsonlite.

Your example is not very replicable, but the below works for me even though, as B.Mr.W notes, there should be a name for those values in JSON.

library(rjson)

no_name <- '["2014-03-01 00:01:54", "2014-03-01 00:04:52", "2014-03-01 00:06:03", 
   "2014-03-01 00:12:11", "2014-03-01 00:14:54", "2014-03-01 00:16:23", "2014-03-01 00:17:19"]'

write(no_name,"temp.json")

fromJSON(file="temp.json") # returns the correct character array

some_json <- '{"timeStamp":["2014-03-01 00:01:54", "2014-03-01 00:04:52", "2014-03-01 00:06:03", 
   "2014-03-01 00:12:11", "2014-03-01 00:14:54", "2014-03-01 00:16:23", "2014-03-01 00:17:19"]}'

fromJSON(some_json) # returns a list with one element, $timeStamp, a character array