2

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?

zx8754
  • 52,746
  • 12
  • 114
  • 209
W.david
  • 21
  • 1
  • I also use Mac, W.david, and your code ran fine for me. Have you opened up your JSON file in a text editor like Atom or Sublime Text to check if there are NULLs or something like that? These multibyte errors usually come from some type of unusual character in the file, like ` ñ `, etc. – RobertMyles Oct 06 '17 at 17:14

2 Answers2

1

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
Henrik
  • 1,101
  • 9
  • 7
-1

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

enter image description here

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135