1

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"]
akrun
  • 874,273
  • 37
  • 540
  • 662
r_beginner
  • 11
  • 3
  • 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. Oct 15 '14 at 17:31
  • May be this link helps `http://stackoverflow.com/questions/21121699/unable-to-convert-json-to-dataframe` – akrun Oct 15 '14 at 17:33

1 Answers1

0

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
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
  • Thanks, John. I was able to get a list with one element(timestamp) by running the command you listed above. 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). But, when I edited my original .json file by adding the key ({"timeStamp":) in front. I still encountered same error (test_data=fromJSON(file='/Users/r_beginer/Desktop/logins.json') Error in is.character(txt) : 'txt' is missing). Any thought on it ? – r_beginner Oct 15 '14 at 21:50