Rupali Shinde
Rupali Shinde

Reputation: 333

SyntaxError: JSON.parse: expected property name or '}' while using highcharts

I am trying to implement a line chart using highcharts, in which I want to color specific points.

So I am using following statement.

JSON.parse("[{x: 1,y: 0},{x:2,y:5,marker:{fillColor:'red'}},{x:3,y:8}]");

to color the point (2,5) as red.

But, it is showing error as SyntaxError: JSON.parse: expected property name or '}'

Upvotes: 16

Views: 34304

Answers (2)

Roman Bekkiev
Roman Bekkiev

Reputation: 3118

As it was said earlier JSON object names must to be quoted. So JSON.parse will parse only that string, valid JSON.

But if you can't for any reason change format of your string you can also parse it using eval function which can accept your syntax. But be careful! That's pretty good way for exploit.

Upvotes: 3

techfoobar
techfoobar

Reputation: 66693

Valid JSON strings require the property names to be quoted.

This can be corrected by quoting the property names like below:

JSON.parse('[{"x": 1, "y": 0}, {"x":2, "y":5, "marker": {"fillColor":"red"}}, {"x":3, "y":8}]');

Upvotes: 33

Related Questions