convert string to JSON

I am working with highcharts and am defining the data in a var called legend_with_value.

If I do so as shown in the first example it works fine, however I am building this data set after making a AJAX call and then extracting the data of interest and trying to build the data set.

Question is how to convert the string

//this works
var legend_with_values =  [['Illinois',5],['Indiana',8],['Kansas',9],['New York',10],['North Dakota',6.7],['Pennsylvania',1]];

//this does not work
x =  "[['Illinois',5],['Indiana',8],['Kansas',9],['New York',10],['North Dakota',6.7],['Pennsylvania',1]]";
legend_with_values = $.toJSON(x);

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Please see

Maybe something like this (untested):
var myObj = {};
myObj.Illinois = "5";
myObj.Indiana = "8";
myObj.Kansas = "9"; // ETC, ETC
var legend_with_value = $.toJSON(myObj);

Open in new window

RobOwner (Aidellio)Commented:
How is the data returned to you from the Ajax call? It's of type String it are you creating the string?
You mentioned you were "building this data set" but can you not just start with an empty js object and build on that?
RobOwner (Aidellio)Commented:
It's your quoting. Single quotes aren't liked by the parser, changing them to double quotes works:

x = '[["Illinois",5],["Indiana",8],["Kansas",9],["New York",10],["North Dakota",6.7],["Pennsylvania",1]]';
legend_with_values = jQuery.parseJSON(x);
legend_with_values= JSON.parse(x);

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobOwner (Aidellio)Commented:
This shows how you can replace all single quotes in the string if you need to
x="testing 'single' quotes";
t=new RegExp("'","g");

Open in new window

lvmllcAuthor Commented:
Perfect - that is all it took.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.