Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 '}'

like image 690
Rupali Shinde Avatar asked Mar 29 '13 07:03

Rupali Shinde


2 Answers

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}]');
like image 112
techfoobar Avatar answered Nov 09 '22 20:11

techfoobar


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.

like image 3
Roman Bekkiev Avatar answered Nov 09 '22 19:11

Roman Bekkiev