convert string to JSON

Posted on 2014-08-01
Medium Priority
Last Modified: 2014-08-02
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

Question by:lvmllc
  • 3
LVL 111

Expert Comment

by:Ray Paseur
ID: 40236031
Please see https://github.com/Krinkle/jquery-json

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

LVL 43

Expert Comment

ID: 40236047
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?
LVL 43

Accepted Solution

Rob earned 1200 total points
ID: 40236052
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

LVL 43

Expert Comment

ID: 40236064
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


Author Closing Comment

ID: 40236912
Perfect - that is all it took.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question