BACFL wrote How do I parse the JSON data and then what type of query do I use to the database so that the zip codes in my results are then displayed on the webpage ordered by distance from the initial zip code? In this instance I queried 5 miles from 34242. For ease, let's presume my database has two columns, "zipcode" and "make" of the vehicle I want to display, i.e. Ford 34242 Nissan 34233 Mazda 34236 Here is the sample JSON for a 5 mile radius of 34242: { "zip_codes": [ ... ] } |
One suitable function to use here is deserializeJSON.
<cfscript>
zipJSON='{
"zip_codes": [
{
"zip_code": "34238",
"distance": 4.647,
"city": "Sarasota",
"state": "FL"
},
{
"zip_code": "34242",
"distance": 0,
"city": "Sarasota",
"state": "FL"
},
{
"zip_code": "34231",
"distance": 1.531,
"city": "Sarasota",
"state": "FL"
},
{
"zip_code": "34233",
"distance": 4.171,
"city": "Sarasota",
"state": "FL"
},
{
"zip_code": "34239",
"distance": 3.698,
"city": "Sarasota",
"state": "FL"
},
{
"zip_code": "34236",
"distance": 3.964,
"city": "Sarasota",
"state": "FL"
},
{
"zip_code": "34230",
"distance": 4.818,
"city": "Sarasota",
"state": "FL"
},
{
"zip_code": "34276",
"distance": 4.818,
"city": "Sarasota",
"state": "FL"
},
{
"zip_code": "34277",
"distance": 4.818,
"city": "Sarasota",
"state": "FL"
},
{
"zip_code": "34278",
"distance": 4.818,
"city": "Sarasota",
"state": "FL"
}
]
}';
zipStruct=deserializeJSON(zipJSON);
/* Enable this dump and you will see a structure containing an array called zip_codes */
// writedump(zipStruct);
/*
The array, zip_codes, contains 10 elements, each a struct.
Here follows an example to obtain the details of the last (10th) item.
*/
//city10=zipStruct.zip_codes[10].city;
//distance10=zipStruct.zip_codes[10].distance;
//state10=zipStruct.zip_codes[10].state;
//zipcode10=zipStruct.zip_codes[10].zip_code;
/*
Alternatively, should you want to obtain each value dynamically:
*/
city=arrayNew(1);
distance=arrayNew(1);
state=arrayNew(1);
zipcode=arrayNew(1);
for(i=1; i lte arrayLen(zipStruct.zip_codes); i=i+1) {
city=zipStruct.zip_codes.city;
distance=zipStruct.zip_codes.distance;
state=zipStruct.zip_codes.state;
zipcode=zipStruct.zip_codes.zip_code;
}
</cfscript>