Search code examples
javascriptjsongoogle-apigoogle-maps-api-2

Is it possible to iterate through a JSON to find if a property exists?


I'm trying to do an experiment with google API geolocation.

and I noticed the JSON is not always the same: sometimes the address is a child of a property, sometimes it is not.

For example, if I want to extract for instance the value of PostalCode, it's sometimes:

...SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber

And sometimes is:

...AdministrativeArea.DependentLocality.Locality.PostalCode.PostalCodeNumber

There is no way to know the exact path ot the value. How can I find the value of PostalCodeNumber independently of its parents?


Solution

  • Try the inJSON plugin here: http://danconnor.com/search-for-a-key-in-json-with-jquery-$.injson.html

    NOTE: I thought I saw jQuery tagged here. Did you remove it or am I seeing things? Anyway, it would be easy to convert this to non-jQuery. It's a simple recursive function.