1

Im having more difficulty with this than I should be!

Im trying to extract postalCode from the below bing maps JSON:

{  
   "authenticationResultCode":"ValidCredentials",
   "brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
   "copyright":"Copyright © 2014 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
   "resourceSets":[  
  {  
     "estimatedTotal":1,
     "resources":[  
        {  
           "__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
           "bbox":[  
              56.216052482429326,
              -2.9494141659354827,
              56.223777917570679,
              -2.9308900340645176
           ],
           "name":"Street, Leven, KY8 5",
           "point":{  
              "type":"Point",
              "coordinates":[  
                 56.2199152,
                 -2.9401521
              ]
           },
           "address":{  
              "addressLine":"Street",
              "adminDistrict":"Scotland",
              "adminDistrict2":"Fife",
              "countryRegion":"United Kingdom",
              "formattedAddress":"Street, Leven, KY8 5",
              "locality":"Leven",
              "postalCode":"KY8 5"
           },
           "confidence":"Medium",
           "entityType":"Address",
           "geocodePoints":[  
              {  
                 "type":"Point",
                 "coordinates":[  
                    56.2199152,
                    -2.9401521
                 ],
                 "calculationMethod":"Interpolation",
                 "usageTypes":[  
                    "Display",
                    "Route"
                 ]
              }
           ],
           "matchCodes":[  
              "Good"
           ]
        }
     ]
  }
   ],
   "statusCode":200,
   "statusDescription":"OK",
   "traceId":"8fdd75362a694e02a45fa17d6e7c0e95|DB40080932|02.00.108.1000|DB4SCH010061257, DB4SCH010061346"
  }

My code only returns the field names and not the attribute:

r = requests.get(current_url)
        json_data = r.json()
        for item in json_data['resourceSets'][0]['resources']:
            for field in item['address']:
                print field

What am I missing? Sorry for the novice question!

1
  • 2
    print field, item['address'][field] Commented Dec 28, 2014 at 13:04

2 Answers 2

2

for field in item['address'] by default iterate through the key in item['address'] (a dictionary) only, so you need to:

for item in json_data['resourceSets'][0]['resources']:
    for field in item['address']:
        print field, item['address'][field]
Sign up to request clarification or add additional context in comments.

Comments

1

For loops over dictionaries in Python iterate just over the keys. If you want the values as well, you should use .items():

 for field, value in item['address'].items():
            print field, value

Comments

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.