11

I get this error when debugging my highcharts javascript code via Firebug. Here are the relevant lines of code:

var valpair = [parseInt(items[0]),cumulative];
rain_series.data.push(JSON.parse(valpair)); 

items[0] is just "1234567", and if I add console.log(valpair) to my code, I get this output: [1234567, 0] which seems to be valid JSON. Nevertheless, I'm stuck on my error (I searched for a solution, but didn't find one for my case).
Anyone here who knows what I'm doing wrong?

1
  • 3
    "which seems to be valid JSON". That's an array, a JavaScript array.
    – Ram
    Feb 2, 2014 at 20:16

1 Answer 1

31

It's because you're using JSON.parse to try to parse an array, which is not going to work. Just get rid of the JSON.parse, and this should work as expected.

When you call JSON.parse on something that's not a string, it's going to coerce it to a string by calling .toString() on it. If valpair = [1234567, 0], then valpair.toString() is going to yield 123457,0 (this is probably dependent on the JS engine: I get 123457,0 in Chrome). This is invalid JSON: the "unexpected non-whitespace character" is the comma.

2
  • 1
    Johabu, if that worked for you, could you please mark my answer as correct? Feb 2, 2014 at 20:17
  • Ah, I didn't know there was a time limit! Sorry to nag! Feb 2, 2014 at 20:28

Your Answer

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

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