0

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

Jeffry
  • 13
  • 4
  • Do you want just an array of all the key values? – Stephen L May 25 '17 at 16:13
  • Is Object.keys(obj.results) not what you're looking for? – anonymouse May 25 '17 at 16:15
  • where are you calling the `getKeys` function? You are doing a function expression and i don't see a function invocation in your posted code. – Abhinav Galodha May 25 '17 at 16:18
  • Let me try Object.keys(obj.results), i will be right back.Stephen, what I need to do is use the lat lng to display marker on map, The key is the order number which will link to the order details page – Jeffry May 25 '17 at 16:18
  • So you basically want an array like: [{orderNumber: '246212': "lat":32.42555,"lng":-96.833181}, .... ] ?? – Stephen L May 25 '17 at 16:22
  • This may help: https://stackoverflow.com/questions/722668/traverse-all-the-nodes-of-a-json-object-tree-with-javascript – Woodrow May 25 '17 at 16:22
  • Object.keys(obj.results) only give me the keys, not the results. The real problem is my lack of knowledge how to parse JS objects – Jeffry May 25 '17 at 16:25
  • Stephen L, yes, exactly, that would be perfect! – Jeffry May 25 '17 at 16:25
  • by the way, i am doing this in jquery cordova app and have underscrore – Jeffry May 25 '17 at 16:28
  • Looks like they closed it... Try this `const orders = obj.results; const orderNumberArray = Object.keys(obj.results) const orderNumbersAndLocations = orderNumberArray.map(ordNum => { return { orderNumber: ordNum, lat: orders[ordNum].response.results[0].location.lat, lng: orders[ordNum].response.results[0].location.lng } })` – Stephen L May 25 '17 at 16:33
  • @Jeffry The question was closed, try to refactor it to explain better your situation, the expected results and what have you tried and post it as a new question, I will be happy to give a solution. – cнŝdk May 25 '17 at 16:45

0 Answers0