2

(R 3.3.1, Windows 10)

Hi - I am trying to pass a string containing double quotes as an argument to an r script (so that I can parse it as JSON using toJSON from the jsonlite package) using the following format from the command prompt:

rscript.exe script.R "argument"

However, I cannot find a way to pass an argument that contains double quotes in a way that it is seen correctly by the script.

The R script I'm using to test is:

library(jsonlite)
args<-commandArgs(trailingOnly=TRUE)
json<-args[1]
print(paste0("Argument received by R as: ", json))
print("JSON validation result:")
validate(json)

I have tried the following with no success (i.e. the quotes are always stripped out or the string is badly escaped in R):

RScript argTest.R "{""x"":1, ""y"":2}"
RScript argTest.R "{"""x""":1, """y""":2}"
RScript argTest.R "{^"x^":1, ^"y^":2}"
RScript argTest.R "{\"x\":1, \"y\":2}"
RScript argTest.R "{\\\"x\\\":1, \\\"y\\\":2}"

The results from my test script for each of these are:

RScript argTest.R "{""x"":1, ""y"":2}"
Loading required package: methods
[1] "Argument received by R as: {x:1, y:2}"
[1] "JSON validation result:"
[1] FALSE
attr(,"err")
[1] "lexical error: invalid char in json text.\n                                      {x:1, y:2}\n                     (right here) ------^\n"

RScript argTest.R "{"""x""":1, """y""":2}"
Loading required package: methods
[1] "Argument received by R as: {x:1, y:2}"
[1] "JSON validation result:"
[1] FALSE
attr(,"err")
[1] "lexical error: invalid char in json text.\n                                      {x:1, y:2}\n                     (right here) ------^\n"


RScript argTest.R "{^"x^":1, ^"y^":2}"
Loading required package: methods
[1] "Argument received by R as: {^x:1, y:2}"
[1] "JSON validation result:"
[1] FALSE
attr(,"err")
[1] "lexical error: invalid char in json text.\n                                      {^x:1, y:2}\n                     (right here) ------^\n"

RScript argTest.R "{\"x\":1, \"y\":2}"
Loading required package: methods
[1] "Argument received by R as: {x:1, y:2}"
[1] "JSON validation result:"
[1] FALSE
attr(,"err")
[1] "lexical error: invalid char in json text.\n                                      {x:1, y:2}\n                     (right here) ------^\n"

RScript argTest.R "{\\\"x\\\":1, \\\"y\\\":2}"
Loading required package: methods
[1] "Argument received by R as: {\"x\":1, \"y\":2}"
[1] "JSON validation result:"
[1] TRUE

This final one looked promising, but if I try to parse it as json, I don't get a useful object, as it appears to "literalise" (I'm sure there is a better word) it:

Additional code:

json_parsed<-toJSON(json)
print(json_parsed)
print(json_parsed$x)

result:

["{\"x\":1, \"y\":2}"]
Error in json_parsed$x : $ operator is invalid for atomic vectors
Calls: print
Execution halted

In production, this will be running on *nix so I think the problem will disappear (yet to be tested), but I need to be able to test in my Windows environment.

Thanks for any help. I've done plenty of searching for this specific issue but not found any useful pointers. I'm not sure whether this is a Windows Shell, Rscript processing, R argument handling, or jsonLite issue.

  • and you could not just put that json string into a file and point the script to the path of the file? – Abdou Aug 09 '16 at 15:18
  • @Abdou The script needs to be passed the json string as part of a pipeline - The json will come from the payload of a message on a message queue (RabbitMQ). Writing it to a file as an intermediate step isn't an option. However, if you can demonstrate successfully piping the contents of a file to the script at the command line then this may be a temporary solution for testing. – Andy Crellin Aug 09 '16 at 15:32
  • How about `"[{\"x\":1, \"y\":2}]"`? – Abdou Aug 09 '16 at 15:55
  • That's equivalent to `"{\"x\":1, \"y\":2}"` except with square brackets added - the double quotes are still removed: `[1] "Argument received by R as: [{x:1, y:2}]"` – Andy Crellin Aug 10 '16 at 10:48
  • I have the exact same problem with R. If I send a json string to Rscript.exe using Bash, you can easily escape the characters and it (just like your fourth example above). However, attempting the same command via Windows CMD or calling Windows Subprocess via Python does not. The inner quotes are always stripped out of the string so Json validation fails. I have tried using the CMD escape character "^" and "\", but no escape character prevents R from removing the inner quotes from the input string. – Jonny Waffles Aug 12 '19 at 15:18

0 Answers0