4

I try reading a JSON file from R using rjson but keep getting errors. I validated the JSON file using various online validators. Here is the content of the JSON file:

{
   "scenarios": [
      {
         "files": {
            "type1": "/home/blah/Desktop/temp/scen_0.type1",
            "type2": "/home/blah/Desktop/temp/scen_0.type2"
         },
         "ID": "scen_0",
         "arr": [],
         "TypeToElementStatsFilename": {
            "type1": "/home/blah/Desktop/temp/scen_0.type1.elements",
            "type2": "/home/blah/Desktop/temp/scen_0.type2.elements"
         }
      }
   ],
   "randomSeed": "39327314969888",
   "zone": {
      "length": 1000000,
      "start": 1
   },
   "instanceFilename": "/home/blah/bloo/data/XY112.zip",
   "txtFilename": "/home/blah/bloo/data/XY112.txt",
   "nSimulations": 2,
   "TypeTodbFilename": {
      "type1": "/home/blah/bloo/data/map.type1.oneAmb.XY112.out"
   },
   "arr": {
      "seg11": {
         "length": 1000,
         "start": 147000
      },
      "seg12": {
         "length": 1000,
         "start": 153000
      },
      "seg5": {
         "length": 1000,
         "start": 145000
      },
      "seg6": {
         "length": 1000,
         "start": 146000
      },
      "seg1": {
         "length": 100,
         "start": 20000
      }
   },
   "outPath": "/home/blah/Desktop/temp",
   "instanceID": "XY112",
   "arrIds": [
      "seg5",
      "seg6",
      "seg1",
      "seg11",
      "seg12"
   ],
   "truth": {
      "files": {
         "type1": "/home/blah/Desktop/temp/truth.type1",
         "type2": "/home/blah/Desktop/temp/truth.type2"
      },
      "ID": "truth",
      "TypeToElementStatsFilename": {
         "type1": "/home/blah/Desktop/temp/truth.type1.elements",
         "type2": "/home/blah/Desktop/temp/truth.type2.elements"
      }
   }
}

And the error:

> json_file <- "~/json"
> json_data <- fromJSON(paste(readLines(json_file), collapse=""))
Error in fromJSON(paste(readLines(json_file), collapse = "")) :
  unexpected character: :
1
  • It would help if you tell us what version of rjson. Is this still an open issue as of 2013? Commented Apr 17, 2013 at 7:38

3 Answers 3

6

RJSON freaks out about empty arrays.

fromJSON( '{ "arr": [ ] }')

Error in fromJSON("{ \"arr\": [ ] }") : unexpected character: :

Sign up to request clarification or add additional context in comments.

1 Comment

+1 thanks. apparently it fails not only on empty arrays, but also on empty objects. I'll send an email to the maintainer, since I think these are valid in JSON.
2

You can try the fromJSON function in the RJSONIO package hosted at http://www.omegahat.org. It seems to read the file fine.

Comments

0

There's a fix for this.

Create a new function to replace the existing getURL function used in RCurl and you should have your solution.

myGetURL <- function(...) {
    rcurlEnv <- getNamespace("RCurl")
    mapUnicodeEscapes <- get("mapUnicodeEscapes", rcurlEnv)
    unlockBinding("mapUnicodeEscapes", rcurlEnv)
    assign("mapUnicodeEscapes", function(str) str, rcurlEnv)
    on.exit({
        assign("mapUnicodeEscapes", mapUnicodeEscapes, rcurlEnv)
        lockBinding("mapUnicodeEscapes", rcurlEnv)
    }, add = TRUE)
    return(getURL(...))
}

Test:

> json <- myGetURL("http://abicky.net/hatena/rcurl/a.json")
> cat(json, fill = TRUE)
{"a":"\\\"\u0030\\\""}
> fromJSON(json)
$a
[1] "\\\"0\\\""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.