0

This is part of my code. The API provides a JSON file that includes an empty value (null) in one location. I get TypeError: 'NoneType' object is not subscriptable on the line that appends. I've read about a number of methods to deal with this (I want my program to just ignore the null value, or replace it with 0 or 'n/a' or whatever, but continue, not break) but they contain insufficient instruction - I still need a lot hand-holding, I'm afraid.

import requests
all_data = []
api_url = 'https://api.nfz.gov.pl/app-umw-api/agreements?page={}&limit=25&format=json&api-version=1.2&serviceType=03&year=2017&branch=07'

for page in range(0, 17):
    data = requests.get(api_url.format(page)).json()

    for a in data["data"]["agreements"]:
        all_data.append({**a["attributes"], "link": a["links"]['related']})
print(all_data)

1 Answer 1

2

Use get instead of direct access:

some_val = json_data.get('host', 'N/A')

Here is how you can achieve this in you code:

import requests
all_data = []
api_url = 'https://api.nfz.gov.pl/app-umw-api/agreements?page={}&limit=25&format=json&api-version=1.2&serviceType=03&year=2017&branch=07'

for page in range(0, 17):
    data = requests.get(api_url.format(page)).json()

    for a in data["data"]["agreements"]:
        res = a.get('links')
        if res is not None:
            res = res.get('related')
        all_data.append({**a["attributes"], "link": res if res is not None else "N/A"})
print(all_data)
Sign up to request clarification or add additional context in comments.

3 Comments

David, thank you, but - sorry - where would I use this in my code? I thought I'm already using get in requests.get...
get is a method for dict it returns None if the key is not found or you can supply your own default value. See my edit
Thank you very much - super helpful. I just made a modification - your edit erased the whole row in which the None value occurred, so I added an 'else' argument that changed that value to "N/A" and then I was able to get rid of the second conditional. Now it does exactly what I needed!

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.