简体   繁体   中英

JSON to JavaScript escape character Query

I am of the understanding that if I am trying to stringify quotes (' and "), i need to escape them but I can't explain the following results when I tried the same out in firebug:

1. >> JSON.stringify({foo: "a"a'a"});
SyntaxError: missing } after property list

Inference: This is expected since I didn't escape " and '

2 >>> JSON.stringify({foo: "a\"a'a"});
 "{"foo":"a\"a'a"}"

Inference/Question: Will the JSON string also show the escape character before " and why it works without escaping the single quote

Also JSON throws an error when I try to parse the output string generated above back to JS object ?

>>> JSON.parse("{"foo":"a\"a'a"}")
SyntaxError: missing ) after argument list

Finally Explain results below: Basically if I escape the single quote once, it doesn't show up in the output string but if I escape twice, it does

>>> JSON.stringify({foo: "a\"a\'a"});
"{"foo":"a\"a'a"}"

>>> JSON.stringify({foo: "a\"a\\'a"});
"{"foo":"a\"a\\'a"}"

Basically I am trying to understand when and how I need to escape single and double quotes when converting to and from JSON. Thanks for your help

EDIT: Thanks for the replies . The first 2 queries are clear. So I only need to escape the quotes I am using to enclose the string ( in my case ") and escape any escape characters itself in the string. Other than these 2, I don't need to escape any other chars?

I am not clear on the last query. If I just increase the escape characters before ', why does it shows even number of escape chars in the output . For eg

 >>> JSON.stringify({foo: "a\"a\'a"});
"{"foo":"a\"a'a"}"
>>> JSON.stringify({foo: "a\"a\\'a"});
"{"foo":"a\"a\\'a"}"
>>> JSON.stringify({foo: "a\"a\\\'a"});
"{"foo":"a\"a\\'a"}"

The format given by your JavaScript interpreter here is a little misleading when it outputs the following:

2 >>> JSON.stringify({foo: "a\"a'a"});
 "{"foo":"a\"a'a"}"

The interpreter is adding the double quotes on the outside without doing any of the necessary escaping to make the result a valid string literal, so what this is actually trying to say is that the result of the expression is a string that contains {"foo":"a\\"a'a"} (where every character there is literal, including the backslash). If you were going to write this as a JavaScript string literal it would be one of the following:

  • With double quotes: "{\\"foo\\":\\"a\\\\\\"a'a\\"}"
  • With single quotes: '{"foo":"a\\\\"a\\'a"}'

The above strings are exactly identical, they are just represented differently based on which external quote is used. You should be able to pass either of those strings to JSON.parse and get an object equivalent to what you started with.

Hopefully this will also help to clarify as to why the single quote isn't escaped, as shown above you only need to escape the type of quote that is used for the string literal (so escape internal double quotes if double quotes surround the string, and escape internal single quotes when single quotes are around the string).

So the errors being thrown is because the string is being ended. So any other characters that follow are attempted to be parsed but aren't able to be. Hence the errors.

So because you start with a quotation mark ("), using an apostrophe (') isn't ending the string. It's within the string, because your string is expected to end with another quotation mark.

If you want to include the same character that the string is defined within, it will need to be escaped. Eg " and he said \\"what a great day!\\" to the other boy"

No need to escape single quotes inside of double quotes, or double quotes inside of single quotes.

You do need to escape like quotes within like quotes -- these are all valid syntax:

var a = "Testing '1234'";
var b = 'Testing "1234"';
var c = "Testing \"1234\"";
var d = 'Testing \'1234\'';

Second piece, on JSON stringification, the double quotes you see output here:

JSON.stringify({foo: "a\"a'a"});
  "{"foo":"a\"a'a"}"

are just an output within whatever console or repl you are using. Technically those should be output as single quotes.

At any rate...

var s = JSON.stringify({foo: "a\"a'a"});
JSON.parse(s);

...will most definitely output a valid object.

Your first inference is correct; you need to escape any special characters (Quotation marks, in this case) that you want to appear in your final string. If you don't, then the browser will try to parse the string as-is, and will fail miserably because of mismatched quotes.

This is the same reason you get an error when you're parsing the string; the parser can't account for the mismatched quotes.

For the last behaviour you were having trouble with, you're not really escaping the quote twice; you're escaping the escape character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM