I have a JSON object received from geocod.io API call. I need to parse the object and get at the key values, particularly the key (e.g., "246212") and the lat and lng
{
"results":{
"246212":{
"query":"1601 Sandhurst Dr, Waxahachie, Texas",
"response":{
"input":{
"address_components":{
"number":"1601",
"street":"Sandhurst",
"formatted_street":"Sandhurst",
"city":"Waxahachie",
"state":"TX",
"zip":"75165",
"country":"US"
},
"formatted_address":"1601 Sandhurst, Waxahachie, TX 75165"
},
"results":[
{
"address_components":{
"number":"1601",
"street":"Sandhurst",
"formatted_street":"Sandhurst",
"city":"Waxahachie",
"county":"Ellis County",
"state":"TX",
"zip":"75165",
"country":"US"
},
"formatted_address":"1601 Sandhurst, Waxahachie, TX 75165",
"location":{
"lat":32.42555,
"lng":-96.833181
},
"accuracy":1,
"accuracy_type":"range_interpolation",
"source":"TIGER\/Line\u00ae dataset from the US Census Bureau"
}
]
}
},
"246994":{
"query":"1415 Willow Run Dr, Duncanville, Texas",
"response":{
"input":{
"address_components":{
"number":"1415",
"street":"Willow Run",
"suffix":"Dr",
"formatted_street":"Willow Run Dr",
"city":"Duncanville",
"state":"TX",
"zip":"75137",
"country":"US"
},
"formatted_address":"1415 Willow Run Dr, Duncanville, TX 75137"
},
"results":[
{
"address_components":{
"street":"Willow Run",
"suffix":"Dr",
"formatted_street":"Willow Run Dr",
"city":"Duncanville",
"county":"Dallas County",
"state":"TX",
"zip":"75137",
"country":"US"
},
"formatted_address":"Willow Run Dr, Duncanville, TX 75137",
"location":{
"lat":32.635994,
"lng":-96.884205
},
"accuracy":0.8,
"accuracy_type":"street_center",
"source":"TIGER\/Line\u00ae dataset from the US Census Bureau"
}
]
}
},
"247037":{
"query":"1800 Windmill Hill Ln, Desoto, Texas",
"response":{
"input":{
"address_components":{
"number":"1800",
"street":"Windmill Hill",
"suffix":"Ln",
"formatted_street":"Windmill Hill Ln",
"city":"Desoto",
"state":"TX",
"zip":"75115",
"country":"US"
},
"formatted_address":"1800 Windmill Hill Ln, Desoto, TX 75115"
},
"results":[
{
"address_components":{
"number":"1800",
"street":"Windmill Hill",
"suffix":"Ln",
"formatted_street":"Windmill Hill Ln",
"city":"Desoto",
"county":"Dallas County",
"state":"TX",
"zip":"75115",
"country":"US"
},
"formatted_address":"1800 Windmill Hill Ln, Desoto, TX 75115",
"location":{
"lat":32.613862,
"lng":-96.907534
},
"accuracy":1,
"accuracy_type":"range_interpolation",
"source":"TIGER\/Line\u00ae dataset from the US Census Bureau"
},
{
"address_components":{
"number":"1800",
"street":"Windmill Hill",
"suffix":"Ln",
"formatted_street":"Windmill Hill Ln",
"city":"Desoto",
"county":"Dallas County",
"state":"TX",
"zip":"75115",
"country":"US"
},
"formatted_address":"1800 Windmill Hill Ln, Desoto, TX 75115",
"location":{
"lat":32.614162,
"lng":-96.907523
},
"accuracy":0.8,
"accuracy_type":"range_interpolation",
"source":"TIGER\/Line\u00ae dataset from the US Census Bureau"
}
]
}
}
}
}
I have searched and tried the following code snippets
// this converts to object
var obj = JSON.parse(geocodedAddress;
console.log('obj ', obj);
for (var key in obj) {
let value = obj[key];
console.log('value ', value);
}
console.log('Object.keys ', Object.keys(obj.results));
// This is not returning anything
var getKeys = function(obj) {
var keys = [];
for (var key in obj) {
keys.push(key);
console.log('key ', key);
}
console.log('keys ', keys);
return keys;
};
I think I am close as you can see from the screenshot. Any help would be much appreciated. enter image description here