1

Wikipedia says that JSON is designed as non-strict subset of JavaScript, that is addition allows usage of some Unicode characters. Quoting current version as of 2015-11-05:

Though JSON is commonly perceived as being a subset of JavaScript and ECMAScript, it allows some unescaped characters in strings that are illegal in JavaScript and ECMAScript strings.

But.. if I paste any of examples from Wikipedia page in ESLint, it fails with error.

For example I paste this:

{
  "id": 1,
  "name": "Foo"
}

Into http://eslint.org/demo/ and get:

2:8 - Parsing error: Unexpected token : (undefined)

Looks like colon is illegal in JavaScript for some reason and it is not about unescaped characters in strings.

Why people still call JSON a JavaScript subset?

4
  • Your JSON is a EcmaScript invalid, but is a valid JSON. Nov 4, 2015 at 19:50
  • It's not so much the colon as an object showing up unexpectedly. Try it prefixed with var x = . (It'll give you an other error, but it's syntactically fine.)
    – Biffen
    Nov 4, 2015 at 19:51
  • try var obj = { "id": 1, "name": "Foo" } Nov 4, 2015 at 19:51
  • just like var x=1 is JS, it doesn't mean it works anywhere, ex var y= var x=2
    – dandavis
    Nov 4, 2015 at 20:07

4 Answers 4

7

JSON is a subset of JavaScript object notation. You can't just declare an object out of nowhere.

The JavaScript usage of that object would be to assign it to a variable:

var o = {
  "id": 1,
  "name": "Foo"
};

or pass it to a function:

console.dir( {"id": 1, "name": "Foo" } );

etc.

0
3

You're running into a somewhat common problem: a loose object literal is interpreted as a block, rather than an object.

If you add var foo = to the beginning, it's perfectly valid:

var foo = {
  "id": 1,
  "name": "Foo"
}

Without the var foo to declare and initialize a variable, the parser sees a block (like if (true) { ... }) and string literal ("id"). The colon is not legal after a loose string literal, because that isn't real JS anywhere:

"id": foo(); // doesn't make sense

This is more often seen with unquoted properties in the object literal, which the parser interprets as named labels (the rarely-used foo: syntax for use with goto). The construct

{
  id: 1,
  name: "Foo"
}

looks like a block with a label id (which points to the expression 1), the comma operator, and another label. Again, that doesn't make much sense, but the first section is at least a legal parse.

Because you've followed the JSON spec rather than JS object literals and quoted your property names, it's picking them up as string literals and can't figure out what to do from there.

Amusingly, if you were to use:

{
  "id".toString(),
  "name".substr(1)
}

then ESlint doesn't complain. It's weird and not very useful, but a completely legal language construct.

2

The issue is that the parser is not able to figure out that it is object context yet. You can wrap it in ():

({
    "id": 1,
    "name": "Foo"
})

it will do what you expect.

1

JSON is a subset because it is meant for data only. JavaScript allows defining functions and executing statements. JSON only allows defining data. That is why it is a subset.

The error you received trying to validate the JSON as a JavaScript statement, is that your data is not a statement. Convert it to a statement to not see the error on eslint:

var foo = {
    "id": 1,
    "name": "Foo"
    };
1
  • above no methods, JSON needs quoted keys, can't encode Date objects or RegExp, or even use object references; only primitives and literals.
    – dandavis
    Nov 4, 2015 at 20:09

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.