Reading JSON string using commandArgs on WIndows

Hello all,
I am having a problem reading a json string from command prompt in Windows using jsonlite. All I want to do is pass in a json string as a command line argument into an R script. I am running my rscript as:

Rscript test-command-line-in-windows.R '{"name":"John","age":31,"city":"New York"}' 

this returns the following error:

json_string contains:
'{name:John,age:31,city:New York}'

Error: lexical error: invalid char in json text.
                                       '{name:John,age:31,city:New Yor
                     (right here) ------^
Execution halted

the file test-command-line-in-windows.R has the following in it:


args <- commandArgs(TRUE)

json_string <- args[1]

# see what is captured by commandArgs
cat("\njson_string contains:\n")
cat(json_string, "\n\n")


It appears that Rscript in windows or just the command prompt in windows is removing all quotes.When I run this on Ubuntu everything works as expected.

Thanks for your help.

This works fine for me on Windows. Output:

$ Rscript test.R '{"name":"John","age":31,"city":"New York"}'

json_string contains:
{"name":"John","age":31,"city":"New York"}

[1] "John"

[1] 31

[1] "New York"

Hi Francis, thanks your help. Are you running it using command prompt?

I used the Terminal pane in RStudio.
I used a different name for your script but I didn't change its contents.
I'm on Windows 10.

Yeah I think the terminal used by Rstudio is bash. Unfortunately this script will need to be run by Rscript in command prompt.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.