2

It is said here:

any quotes inside the JSON have to be “escaped” with a backslash in front. Otherwise, JavaScript gets confused about which quotes we want for display and which quotes are part of the programming.

but in their code snippet I can't see any escaping character is this tutorial buggy I'm confused ? :

var movielisttext = "{"movielist": ["Friday the 13th", "Friday the 13th Part 2", "Friday the 13th Part III", "Friday the 13th: The Final Chapter", "Friday the 13th: A New Beginning"]}";

My question is specifically if their article has an error or not because it amazes me that a tutorial for beginner can embed such error.

user310291
  • 36,946
  • 82
  • 271
  • 487

4 Answers4

4

What you have is JavaScript, not JSON.

If you want JSON:

{
    "movielist": [
        "Friday the 13th",
        "Friday the 13th Part 2",
        "Friday the 13th Part III",
        "Friday the 13th: The Final Chapter",
        "Friday the 13th: A New Beginning"
    ]
}

If you want a JavaScript object

var movielisttext =   {
        "movielist": [
            "Friday the 13th",
            "Friday the 13th Part 2",
            "Friday the 13th Part III",
            "Friday the 13th: The Final Chapter",
            "Friday the 13th: A New Beginning"
        ]
    };

If you want a JavaScript string containing the JSON:

var movielisttext =  '{"movielist": ["Friday the 13th","Friday the 13th Part 2","Friday the 13th Part III","Friday the 13th: The Final Chapter","Friday the 13th: A New Beginning"]}';

or

var movielisttext = "{\"movielist\": [\"Friday the 13th\",\"Friday the 13th Part 2\",\"Friday the 13th Part III\",\"Friday the 13th: The Final Chapter\",\"Friday the 13th: A New Beginning\"]}";

Since the data itself doesn't include any " characters, they don't need to be escaped as far as the JSON is concerned.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • And what if the data itself contains " and ' ? – user310291 Nov 06 '10 at 18:14
  • If the data contains `"` characters, then they will need to be escaped. The `'` won't since they aren't delimiters. If you then want to use that as a string in JS (as per the last example) then you'll need to escape the quotes (again) and also escape (in JS) the (JSON) escape sequences. – Quentin Nov 06 '10 at 18:30
2

Since this is JavaScript and not JSON, just omit the surrounding quotes:

var movielisttext = {"movielist": ["Friday the 13th", "Friday the 13th Part 2", "Friday the 13th Part III", "Friday the 13th: The Final Chapter", "Friday the 13th: A New Beginning"]};
Gumbo
  • 643,351
  • 109
  • 780
  • 844
1

Strings in JSON must always be wrapped in double quotes. In that example they should have formatted the JSON like this:

var movielisttext = '{"movielist": ["Friday the 13th", "Friday the 13th Part 2", "Friday the 13th Part III", "Friday the 13th: The Final Chapter", "Friday the 13th: A New Beginning"]}';

But if their intention was to create a literal Javascript object, they should have used:

var movielisttext = {"movielist": ["Friday the 13th", "Friday the 13th Part 2", "Friday the 13th Part III", "Friday the 13th: The Final Chapter", "Friday the 13th: A New Beginning"]};

In the first case, the value of movielisttext is a string, in the second case it's an object

Harmen
  • 22,092
  • 4
  • 54
  • 76
1
var movielisttext = '{"movielist": ["Friday the 13th", "Friday the 13th Part 2", "Friday the 13th Part III", "Friday the 13th: The Final Chapter", "Friday the 13th: A New Beginning"]}';

OR

var movielisttext = "{\"movielist\": [\"Friday the 13th\", \"Friday the 13th Part 2\", \"Friday the 13th Part III\", \"Friday the 13th: The Final Chapter\", \"Friday the 13th: A New Beginning\"]}";

Would do the trick.

partoa
  • 900
  • 13
  • 22