tl;dr
If you're inside javascript, python, etc., use raw strings (python's r''
or javascript's String.raw
or similar). They make it much easier to write JSON strings because they avoid multiple escape sequence processing.
console.log(JSON.parse(String.raw`"This is a double quote >> \" <<"`))
// => This is a double quote >> " <<
More in depth
Some confusion when writing JSON strings in code comes from string escape sequences being processed multiple times. One time in the programming language, again in the JSON parser (e.g. JSON.parse()
in Javascript, or similar)
Use the language's print function to see what escapes are happening in the programming language
It can be confusing to see how strings are displayed in a programming language repl.
E.g. when you type a string directly into a javascript repl, it displays it this way
'Two newlines:\n\nTab here >>\t<<\n\nBackslash here >>\\<<'
// => 'Two newlines:\n\nTab here >>\t<<\n\nBackslash here >>\\<<'
But when you console.log()
the string, it displays it this way
console.log('Two newlines:\n\nTab here >>\t<<\n\nBackslash here >>\\<<')
/* =>
Two newlines:
Tab here >> <<
Backslash here >>\<<
*/
When javascript encounters a string, it 'evaluates' the escape sequences before passing it e.g. to a function, in the sense that it replaces each \n
with a newline character, each \t
with a tab character, etc.
So it helps a lot to console.log()
the string to get a better idea of what's going on.
How to encode a single quote in JSON in javascript
To write a "
to a JSON in javascript, you could use
console.log(JSON.parse('"This is a double quote >> \\" <<"'));
// => This is a double quote >> " <<
It'd be similar in python and other languages.
Step by step:
- javascript evaluates the string using escape sequence rules from the javascript spec, replacing
\n
with a newline char, \t
with a tab char, etc.
- In our case, it replaces
\\
with \
.
- The result string is
"This is a double quote >> \" <<"
- We put the outer double quotes to make it a valid JSON string
- javascript takes the result and passes it to the JSON.parse() fn.
- JSON.parse evaluates the string using escape sequence rules from the JSON standard, replacing
\n
with a newline char, \t
with a tab char, etc. In our case,
- the first character it sees is
"
, so it knows this is a JSON string.
- Inside the JSON string, it sees
\"
. Normally "
would end the JSON string, but because "
is escaped with \
, it knows this isn't the end of the string and to replace \"
with a literal double quote character.
- the last character it sees is
"
, so it knows this is the end of the JSON string
- The result parsed string is
This is a double quote >> " <<
. Note the outer double quotes are gone also.
Raw strings make things easier
Javascript's String.raw
template function and python's r''
strings don't do any escape sequence evaluating, so it makes it much easier and less confusing to reason about
console.log(JSON.parse(String.raw`"This is a double quote >> \" <<"`))
// => This is a double quote >> " <<
All characters may be placed within the quotation marks except for the characters that must be escaped
and then it specifies:\" represents the quotation mark character (U+0022)
– Kalmar