How to read data from a JSON object (in a file) in R?

I am new to R and am not much affected by programming. I am having a problem loading a file (contains a JSON object) in R.

> library(rjson)
> jsonFile <- "C:\\Users\\jsonRecords.txt"
> jsonData <- fromJSON( jsonFile, method = "C", unexpected.escape = "error" )
Error in fromJSON(jsonFile, method = "C", unexpected.escape = "error") : 
  unexpected character 'C'

I wanted the data to be read in R for further analysis. Any help would be appreciated.

thank

+5
source share
2 answers

Try the following:

    fromJSON( file = json_file )

It will read the whole file. here is an example:

write(toJSON( iris ),'jstest')
res <- fromJSON( file="jstest")

str(res)
List of 5
 $ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : chr [1:150] "setosa" "setosa" "setosa" "setosa" ...
+11
source

it looks like all you are missing is an argument file=

fromJSON( file = json_file, method = "C", unexpected.escape = "error" )

if you look args(fromJSON)

 > args(fromJSON)
 function (json_str, file, method = "C", unexpected.escape = "error") 

, json_str, - file. , , . ( , json_file json-, .. , .)

+5

All Articles