RJSONIO fromJSON error

417 Views Asked by At

I have a problem with the fromJSON function in RJSONIO package in R.

I have a json file to read with fromJSON

{"indy movies" :[
{
"name" : "Raiders of the Lost Ark",
"year" : 1981,
"actors" : {
    "Indiana Jones": "Harrison Ford", 
    "Dr. René Belloq": "Paul Freeman" 
    },
"producers": ["Frank Marshall", "George Lucas", "Howard Kazanjian"],
"budget" : 18000000,
"academy_award_ve": true
},
{
"name" : "Indiana Jones and the Temple of Doom",
"year" : 1984,
"actors" : {
    "Indiana Jones": "Harrison Ford", 
    "Mola Ram": "Amish Puri"
    },
"producers": ["Robert Watts"],
"budget" : 28170000,
"academy_award_ve": true
},
{
"name" : "Indiana Jones and the Last Crusade",
"year" : 1989,
"actors" : {
    "Indiana Jones": "Harrison Ford", 
    "Walter Donovan": "Julian Glover"
    },
"producers": ["Robert Watts", "George Lucas"],
"budget" : 48000000,
"academy_award_ve": false
}]}

The file name is "indy.json"

Here is a reproducible example:

indy <- fromJSON(content = "indy.json")

However,i get the result:

> indy <- fromJSON(content = "indy.json")
Error in nchar(content) : invalid multibyte string, element 1

Here is my relevant sessionInfo()

R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.4

Can anyone please suggest why this is happening?

2

There are 2 best solutions below

0
Henrik On

Try using the jsonlite package instead. I pasted your string in a file, saved it as t.R, then read it back in. The string is represented as yours above.

> library(jsonlite)
> library(readr)
> x <- read_file("t.R")
> x
[1] "{\"indy movies\" :[\r\n  {\r\n    \"name\" : \"Raiders of the Lost Ark\",\r\n    \"year\" : 1981,\r\n    \"actors\" : {\r\n      \"Indiana Jones\": \"Harrison Ford\", \r\n      \"Dr. René Belloq\": \"Paul Freeman\" \r\n    },\r\n    \"producers\": [\"Frank Marshall\", \"George Lucas\", \"Howard Kazanjian\"],\r\n    \"budget\" : 18000000,\r\n    \"academy_award_ve\": true\r\n  },\r\n  {\r\n    \"name\" : \"Indiana Jones and the Temple of Doom\",\r\n    \"year\" : 1984,\r\n    \"actors\" : {\r\n      \"Indiana Jones\": \"Harrison Ford\", \r\n      \"Mola Ram\": \"Amish Puri\"\r\n    },\r\n    \"producers\": [\"Robert Watts\"],\r\n    \"budget\" : 28170000,\r\n    \"academy_award_ve\": true\r\n  },\r\n  {\r\n    \"name\" : \"Indiana Jones and the Last Crusade\",\r\n    \"year\" : 1989,\r\n    \"actors\" : {\r\n      \"Indiana Jones\": \"Harrison Ford\", \r\n      \"Walter Donovan\": \"Julian Glover\"\r\n    },\r\n    \"producers\": [\"Robert Watts\", \"George Lucas\"],\r\n    \"budget\" : 48000000,\r\n    \"academy_award_ve\": false\r\n  }]}"
> jsonlite::fromJSON(x)
$`indy movies`
                                  name year actors.Indiana Jones actors.Dr. René Belloq actors.Mola Ram
1              Raiders of the Lost Ark 1981        Harrison Ford           Paul Freeman            <NA>
2 Indiana Jones and the Temple of Doom 1984        Harrison Ford                   <NA>      Amish Puri
3   Indiana Jones and the Last Crusade 1989        Harrison Ford                   <NA>            <NA>
  actors.Walter Donovan                                      producers   budget academy_award_ve
1                  <NA> Frank Marshall, George Lucas, Howard Kazanjian 18000000             TRUE
2                  <NA>                                   Robert Watts 28170000             TRUE
3         Julian Glover                     Robert Watts, George Lucas 48000000            FALSE
0
user127205 On

There are some Invisible characters in indy.json Delete it, the problem will be solved.

enter image description here