2

In chrome, using this code

JSON.parse('[ "Rectangle", { width: 10 } ]')

throws me a

SyntaxError: Unexpected token w

Internet Explorer 11(.0.9600) has a similar behaviour: "Invalid character".

According to the definitions at http://json.org/, this syntax is clearly allowed and boils down to an array containing two elements: a string "Rectangle" and an object with one single property width and its value 10

What's wrong with it?

1
  • 1
    Asad is right. FYI, the exact same error occurs in Chrome (and should occur in other browsers). Commented Mar 21, 2014 at 5:23

2 Answers 2

4

In JSON, object keys are strings, and therefore need to have quotes around them

JSON.parse('[ "Rectangle", { "width": 10 } ]')
Sign up to request clarification or add additional context in comments.

Comments

0

Another option is to convert it to proper JSON first:

let obj = ["Rectangle",{ width: 10 }];
obj = JSON.parse(JSON.stringify(obj));

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.