0

I am trying to print json values in python like below from the json output I receive from the API.

POSTCODE : CB3 0FA COUNTRY : England REGION : East of England

POSTCODE : CB3 0GT COUNTRY : England REGION : East of England

POSTCODE : CB3 0FT COUNTRY : England REGION : East of England

def loadJsonResponse(url):
    return json.loads(req.urlopen(url).read().decode('utf-8'))['result']


def nearestPostcode(postcode):
    url = 'https://api.postcodes.io/postcodes/{}/nearest'.format(postcode)
    value = loadJsonResponse(url)
    for i in value['result']:
        zip_code = i['postcode']
        print (zip_code)

Below is my Json output:

{
"status": 200,
"result": [
    {
        "postcode": "CB3 0FA",
        "quality": 1,
        "eastings": 542934,
        "northings": 258794,
        "country": "England",
        "nhs_ha": "East of England",
        "longitude": 0.090435,
        "latitude": 52.208837,
        "european_electoral_region": "Eastern",
        "primary_care_trust": "Cambridgeshire",
        "region": "East of England",
        "lsoa": "Cambridge 007D",
        "msoa": "Cambridge 007",
        "incode": "0FA",
        "outcode": "CB3",
        "distance": 0,
        "parliamentary_constituency": "Cambridge",
        "admin_district": "Cambridge",
        "parish": "Cambridge, unparished area",
        "admin_county": "Cambridgeshire",
        "admin_ward": "Newnham",
        "ccg": "NHS Cambridgeshire and Peterborough",
        "nuts": "Cambridgeshire CC",
        "codes": {
            "admin_district": "E07000008",
            "admin_county": "E10000003",
            "admin_ward": "E05002710",
            "parish": "E43000042",
            "parliamentary_constituency": "E14000617",
            "ccg": "E38000026",
            "nuts": "UKH12"
        }
    },
    {
        "postcode": "CB3 0GT",
        "quality": 1,
        "eastings": 542895,
        "northings": 258788,
        "country": "England",
        "nhs_ha": "East of England",
        "longitude": 0.089862,
        "latitude": 52.208793,
        "european_electoral_region": "Eastern",
        "primary_care_trust": "Cambridgeshire",
        "region": "East of England",
        "lsoa": "Cambridge 007D",
        "msoa": "Cambridge 007",
        "incode": "0GT",
        "outcode": "CB3",
        "distance": 39.47393492,
        "parliamentary_constituency": "Cambridge",
        "admin_district": "Cambridge",
        "parish": "Cambridge, unparished area",
        "admin_county": "Cambridgeshire",
        "admin_ward": "Newnham",
        "ccg": "NHS Cambridgeshire and Peterborough",
        "nuts": "Cambridgeshire CC",
        "codes": {
            "admin_district": "E07000008",
            "admin_county": "E10000003",
            "admin_ward": "E05002710",
            "parish": "E43000042",
            "parliamentary_constituency": "E14000617",
            "ccg": "E38000026",
            "nuts": "UKH12"
        }
    },
    {
        "postcode": "CB3 0FT",
        "quality": 1,
        "eastings": 542856,
        "northings": 258824,
        "country": "England",
        "nhs_ha": "East of England",
        "longitude": 0.089307,
        "latitude": 52.209126,
        "european_electoral_region": "Eastern",
        "primary_care_trust": "Cambridgeshire",
        "region": "East of England",
        "lsoa": "Cambridge 007D",
        "msoa": "Cambridge 007",
        "incode": "0FT",
        "outcode": "CB3",
        "distance": 83.54443275,
        "parliamentary_constituency": "Cambridge",
        "admin_district": "Cambridge",
        "parish": "Cambridge, unparished area",
        "admin_county": "Cambridgeshire",
        "admin_ward": "Newnham",
        "ccg": "NHS Cambridgeshire and Peterborough",
        "nuts": "Cambridgeshire CC",
        "codes": {
            "admin_district": "E07000008",
            "admin_county": "E10000003",
            "admin_ward": "E05002710",
            "parish": "E43000042",
            "parliamentary_constituency": "E14000617",
            "ccg": "E38000026",
            "nuts": "UKH12"
        }
    }
]
}

I see below error on execution of the script.

Enter the postcode : CB3 0FA
You entered postcode--> CB3 0FA
Traceback (most recent call last):
  File "./py_script3.py", line 62, in <module>
    nearestPostcode(postcode)
  File "./py_script3.py", line 42, in nearestPostcode
    for i in value['result']:
TypeError: list indices must be integers or slices, not str
2
  • Please explain what is the problem here. Commented Oct 11, 2018 at 17:31
  • I added the error in main question at the end.. Sorry! Commented Oct 11, 2018 at 17:45

1 Answer 1

1

This should work, using formatted print, tested by loading your json output

for i in value['result']:
    print('POSTCODE: {} England REGION: {}'.format(i['postcode'], i['region']))

POSTCODE: CB3 0FA England REGION: East of England
POSTCODE: CB3 0GT England REGION: East of England
POSTCODE: CB3 0FT England REGION: East of England
Sign up to request clarification or add additional context in comments.

6 Comments

Enter the postcode : CB3 0FA You entered postcode--> CB3 0FA Traceback (most recent call last): File "./py_script3.py", line 62, in <module> nearestPostcode(postcode) File "./py_script3.py", line 42, in nearestPostcode for i in value['result']: TypeError: list indices must be integers or slices, not str
I added in the main question, my apologies
@garg_hbti I want to say the issue is how you are loading the json, can you fix the indentation on your post and show how you are calling nearest postcode
@garg_hbti if you save that file into a blah.json and then load it, it works so it has to be something with how you are loading it, not the actual looping of the contents
The miss was ,I was returning the result section directly from the loadJsonResponse(url), once I removed the ['result'] section from there it worked like a charm. Thanks!!
|

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.