Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct label syntax for JSON object

It seems there are different standards out there on labels for JSON, some want quotes around JSON object labels, some do not. Can someone tell me what the standard is?

Quotes are bad camp

Chrome

{"label":1111} - SyntaxError: Unexpected token :

{label:1111} - Works

Firefox

{"label":1111} - SyntaxError: invalid label

{label:1111} - Works

Quotes are good camp

JSLint

{"video_id":1111} - JSON: good.

{video_id:1111} - JSON: bad. Expected a string and instead saw 'video_id'

PHP

echo json_encode(array('label' => 1111));
{"label":1111}
like image 793
quickshiftin Avatar asked Jan 30 '26 18:01

quickshiftin


1 Answers

The standard is to parse JSON as JSON.

The JSON language (unlike Javascript) always requires all property names to be surrounded by double-quotes.

Your syntax errors come from trying to parse JSON as Javascript statements. The {} is parsed as a statement block, and the label: is parsed as a GOTO target.
Since statement labels cannot have quotes, this results in a syntax error.

If you wrap the JSON literals in parentheses to force Javascript to parse them as expressions, you won't get that error.

like image 50
SLaks Avatar answered Feb 02 '26 10:02

SLaks