1

I'm trying to export some data generated by our server, and import it into Googles Column Chart.

I've read the documentation regarding using JSON to import from the server, however, I'm not using a pie chart, and they haven't given much information about how they generate the JSON, and the format it quite different between a pie and column chart.

My server is currently returning the following code:

[['Week', 'Present', 'Absent', 'Other'], 
 ['Week 1', 2, 0, 0],
 ['Week 2', 0, 0, 0],
 ['Week 3', 0, 0, 0],
 ['Week 4', 0, 0, 0],
 ['Week 5', 0, 0, 0]
]

Now, if I import this directly into the charts:

google.visualization.arrayToDataTable(response);

Then I get the error message 'Not an array'. Which I guess is true, actually being a string.

However, after googling, It would appear that using $.parseJSON would work. I've use this function on another Line graph google chart and its worked, however whenever I use $.parseJSON(response), all I get is

Uncaught SyntaxError: Unexpected token '

I cannot for the life of me see where the apostrophe is. All I want to do is import my server generated code into a chart.

Any help is always appreciated.

0

2 Answers 2

6

For $.parseJSON you need to use " insted of '.

    var formatted = original.replace(/'/g, '"');

Have a look at this JSFiddle: http://jsfiddle.net/7pdgn/

Sign up to request clarification or add additional context in comments.

2 Comments

Unbelievable! Thank you very much for the answer, you've saved me from pulling out a lot of hair! I'll Accept the answer when I can
It took me a while to figure this out myself a few months ago, I have to admit it's pretty annoying.
3

That isn't valid JSON; JSON strings must be double-quoted.

You should change your server to use an actual JSON serializer instead of building strings by hand.

1 Comment

Thanks @slaks very easy mistake to make. Fortunately, I dont need to use much JSON. Thank you very much for your quick answer :)

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.