[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


parse JSON from Ajax pull in JQUERY

Posted on 2012-09-15
Medium Priority
Last Modified: 2012-09-16

I have a large JSON with 30 elements coming back as a JSON array via JQUERY:

                 url: '../../services/markers.php',
                 method: "get",
                 dataType: 'json',
                 data: 'name='+name+'&loc='+loc+'&range='+range+'&filter1='+filter1+'&filter2='+filter2,
                 success: function(Markerdata, status, xhr) {
                              Do task 1
                              Do task 2


A typical result:

"ID" :      "3266",
"ProviderID" :      "360159",
"Address" :      "272 HOSPITAL ROAD",
"City" :      "CHILLICOTHE",
"St" :      "OH",
"ZIP" :      "45601",
"County" :      "ROSS",
"PhnNum" :      "7407797358",
"Type" :      "Acute Care Hospitals",
"Owner" :      "Voluntary non-profit - Private",
"ER" :      "true",
"Lat" :      "39.395330000",
"Lng" :      "-82.970539000",
"Tier1or2" :      "1",
"Academic" :      "0",
"Cmpsn200plus" :      "1",
"CME" :      "0",
"MjDblCvg" :      "0",
"MdLvlCvg" :      "1",
"EMR" :      "0",
"NoIHCdCvg" :      "0",
"NoAdmOrdrs" :      "1",
"PriSnglCvg" :      "1",
"AnyStLic" :      "1",
"RecStatus" :      "1",
"Reprentation" :      "1",
"OtherInfo" :      "",
"data" :      "<h2>ADENA REGIONAL MEDICAL CENTER</h2><p>272 HOSPITAL ROAD<br />CHILLICOTHE, OH 45601<br />(740) 779-7358<br /><button ID=&quot;3266&quot;>See More Information</button></p>"


This can come back in 1 record to 100s of records.
I use this JSON to perform multiple tasks using one data pull.

What I need tho, is as one of the tasks to pull a subset out of this set into it's own JSON.

I need Lat, Lng, and Data beside itself.
Yes, it's for a google map.

How would I grep the JSON to create a new json with just the three columns?
I don't want to change the data pull, because Tasks 2-4 have need of the rest of the columns and I only want to do one data pull.

Thanks much.
Question by:Evan Cutler
  • 2
  • 2
LVL 25

Accepted Solution

lwadwell earned 2000 total points
ID: 38402618
You could just assign the data to a new variable in a loop, e.g.
    var subsetJSON = new Array;
    for ( i=0; i<Markerdata.length; i++ ) {
        subsetJSON[i] = { "Lat"  : Markerdata[i].Lat
                         ,"Lng"  : Markerdata[i].Lng
                         ,"data" : Markerdata[i].data

Open in new window


Author Closing Comment

by:Evan Cutler
ID: 38404077
You know,
it's always the easy stuff that gets you.

That is an awesome answer.
My dumb luck, the code I would have made would be 85 lines long.

Thanks much.
LVL 25

Expert Comment

ID: 38404087
>> You know, it's always the easy stuff that gets you.
Yup ... totally agree!  When your head down working hard on a solution it is way too easy to over-think every problem ... sometimes you need to stop, talk a walk and come back with a clear mind.
Thanks for the points and best of luck with the rest of your code.

Author Comment

by:Evan Cutler
ID: 38404088
Yeah,  That's when I stop and "offer points"  sometimes...lol

Thanks much

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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 Month19 days, 9 hours left to enroll

872 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