Closed dagtann closed 3 years ago
It is not double escaped, a backslash needs to be escaped in json and that is what happens. You may be confused by how R prints strings?
x = "foo\\bar"
cat(x, "\n")
cat(jsonlite::toJSON(x), "\n")
identical(x, jsonlite::fromJSON(jsonlite::toJSON(x)))
The output file my_file.txt contains a single line:
["foo\\bar"]
.
Yes that is the only correct json. If it would be ["foo\bar"]
then the \b
would be parsed as a backspace character.
I am not confused by R's internal representation. However, your hint that "foo\\bar" is the only correct JSON lead me to the source of the problem: https://www.tutorialspoint.com/json_simple/json_simple_escape_characters.htm. Thank you!
Summary
When pushing character vectors that contain literal backslashes, e.g.,
"foo\bar"
throughjsonlite::toJSON
, the resulting array will be"foo\\bar"
.Could some internal jsonlite function lead to doubly escaped backslashes?
Repex
The output file my_file.txt contains a single line:
["foo\\bar"]
.Session information