1

I have the following JSON file which stores geocode_output

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "277",
               "short_name" : "277",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Bedford Avenue",
               "short_name" : "Bedford Ave",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Williamsburg",
               "short_name" : "Williamsburg",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Brooklyn",
               "short_name" : "Brooklyn",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Kings County",
               "short_name" : "Kings County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "11211",
               "short_name" : "11211",
               "types" : [ "postal_code" ]
            }
         ]
       }

I am trying to return the value in the value "11211" from the "long_name" in the "postal_code" type field at the bottom of the file.

How should I parse it and return the value as a string?

Any help will be appreciated.

3
  • You may want to look at JSONPath, dpath, and KVC, three different APIs for searching JSON-style nested structures with a nice path syntax, which all have Python modules on PyPI. Commented May 12, 2018 at 6:27
  • If not, just loop. Break it down step by step, and all the parts are easy. results = d['results'], then for result in results:, then components = result['address_components'], then for component in components:, then you can do things like if 'postal_code' in component['types']:. Commented May 12, 2018 at 6:29
  • @abarnert When I try to break down the parts step by step I get the following error "TypeError: string indices must be integers, not str." Any idea why? Commented May 12, 2018 at 16:43

1 Answer 1

2

You can iterate over your JSON.

Ex:

for address in data["results"]:
    for i in address["address_components"]:
        if i["long_name"] == "11211":
            print(i)

Output:

{'long_name': '11211', 'types': ['postal_code'], 'short_name': '11211'}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Rakesh. When I run that I get the following error message "TypeError: string indices must be integers, not str." Any idea why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.