Ryan Peterson
Ryan Peterson

Reputation: 142

JSON Array from String

I have an array of JSON plots which I store in MySQL. When I retrieve this information from MySQL it is given as one long string. How can I restore this back into an array of JSON objects using Javascript? I'm running this using NodeJS and MySQL package.

My data is returned like the following:

'[{"x":0,"y":0},{"x":1,y:1},{"x":2,"y":2}]'

What I would like to be able to do is use the data like:

var data = [{"x":0,"y":0},{"x":1,"y":1},{"x":2,"y":2}];
console.log(data[0].x);

I've had a try using JSON.parse and originally stored the data using JSON.stringify on the array, but it is not behaving as I would expect.

Are there any methods or packages available to handle this?

Edit: I realize now that this is not JSON but rather objects. Apologies for the wrong terminology here, but my problem still remains.

Upvotes: 1

Views: 173

Answers (1)

Marcin Malinowski
Marcin Malinowski

Reputation: 795

var data = new Function ('return ' + dataString)();

Upvotes: 1

Related Questions