Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird JSON parsing behavior in js, "Unexpected token :"

As demonstrated in this jsfiddle, if you have a JS file and you create a JSON object without using it, it behaves differently depending on whether the keys(members) are wrapped in quotes or not.

valid code:{ a: 1};
invalid code: { "a": 1 };

What you will get is an error message (in Chrome, different for FF/IE, but still fails on syntax)

Uncaught SyntaxError: Unexpected token :

but if you use the object in some way, for example: alert({ "a": 1 }); everything is OK again.

Why does this happen?

like image 350
karnyj Avatar asked Apr 09 '12 22:04

karnyj


People also ask

How do I fix unexpected token in JSON error?

The "Unexpected token u in JSON at position 0" error occurs when we pass an undefined value to the JSON. parse or $. parseJSON methods. To solve the error, inspect the value you're trying to parse and make sure it's a valid JSON string before parsing it.

What is JSON parse?

JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON. parse() , as the Javascript standard specifies.

What is JSON parse and JSON Stringify?

parse() is used for parsing data that was received as JSON; it deserializes a JSON string into a JavaScript object. JSON. stringify() on the other hand is used to create a JSON string out of an object or array; it serializes a JavaScript object into a JSON string. Follow this answer to receive notifications.


3 Answers

The statement:

{ a: 1 };

is not an object literal. It's a block statement with one labeled expression in it. It's valid.

This:

{ "a": 1 };

is a syntax error because it's just not parseable. The quoted "a" starts an expression statement inside the block, but then the next token after the string is a colon, and there's no expression form that looks like an expression followed by a colon.

Now:

var x = { "a": 1 };

works because the "{" is not interpreted as the start of a block statement. That statement starts with var, so it's a variable declaration. Within the expression on the right side of the "=" token, the only thing that a "{" can mean is the start of an object literal. Similarly, note that:

({ "a": 1 });

is OK because the opening parenthesis makes the parser expect a nested subexpression, so again the "{" unambiguously means that it's the start of an object literal.

like image 136
Pointy Avatar answered Oct 08 '22 20:10

Pointy


I just realized than when loading the JSON via require and the filename does not end on .json i get this error. Renaming the file to bla.json and it works fine.

like image 20
SCBuergel Avatar answered Oct 08 '22 21:10

SCBuergel


This error can popup when doing a jQuery AJAX call using jsonp when jsonp is not necessary. Try switching your data type on your AJAX call if this is the case to normal json

$.ajax({
     dataType: 'json', // try using json rather than json p
     ...
});
like image 2
Chris Klingler Avatar answered Oct 08 '22 20:10

Chris Klingler