JSON.parse unexpected token s
Asked Answered
L

5

53

Why is it that whenever I do :-

JSON.parse('"something"')

it just parses fine but when I do:-

var m = "something";
JSON.parse(m);

it gives me an error saying:-

Unexpected token s
Libreville answered 13/9, 2013 at 17:10 Comment(3)
"something" is not valid JSON (it doesn't include the quotes).Osteology
@SLaks parse it like normal string but from all the responses string literal is not a valid JSONstring.Libreville
It's a string already. There isn't anything to parse.Multiplex
M
68

You're asking it to parse the JSON text something (not "something"). That's invalid JSON, strings must be in double quotes.

If you want an equivalent to your first example:

var s = '"something"';
var result = JSON.parse(s);
Mulholland answered 13/9, 2013 at 17:12 Comment(3)
that works - but I thought that JS allowed single and double quotes without prejudice. Good to know though.Enthronement
@reabow: JavaScript allows single or double quotes. JSON does not.Mulholland
@T.J.Crowder Thanks, JSON is pretty particular, especially after you are used to python dicts - maybe it should be called Just Someother Object Notation then :)Enthronement
K
20

What you are passing to JSON.parse method must be a valid JSON after removing the wrapping quotes for string.

so something is not a valid JSON but "something" is.

A valid JSON is -

JSON = null
    /* boolean literal */
    or true or false
    /* A JavaScript Number Leading zeroes are prohibited; a decimal point must be followed by at least one digit.*/
    or JSONNumber
    /* Only a limited sets of characters may be escaped; certain control characters are prohibited; the Unicode line separator (U+2028) and paragraph separator (U+2029) characters are permitted; strings must be double-quoted.*/
    or JSONString

    /* Property names must be double-quoted strings; trailing commas are forbidden. */
    or JSONObject
    or JSONArray

Examples -

JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null 
JSON.parse("'foo'"); // error since string should be wrapped by double quotes

You may want to look JSON.

Kindness answered 13/9, 2013 at 17:13 Comment(0)
C
7

Variables (something) are not valid JSON, verify using http://jsonlint.com/

Conners answered 13/9, 2013 at 17:11 Comment(0)
C
4

valid json string must have double quote.

JSON.parse({"u1":1000,"u2":1100})       // will be ok

no quote cause error

JSON.parse({u1:1000,u2:1100})    
// error Uncaught SyntaxError: Unexpected token u in JSON at position 2

single quote cause error

JSON.parse({'u1':1000,'u2':1100})    
// error Uncaught SyntaxError: Unexpected token u in JSON at position 2

You must valid json string at https://jsonlint.com

Coelacanth answered 6/6, 2018 at 23:15 Comment(0)
G
2

Because JSON has a string data type (which is practically anything between " and "). It does not have a data type that matches something

Goer answered 13/9, 2013 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.