skip to Main Content

I have an API which returns null when there are no results, else it’s usually got a count with an int. In this minimal example with a similar data structure, if either value is null I’d hope to return 0.

I thought that adding {} usually allows traversing deeper into the data I get the following:

Traceback (most recent call last): File
"/home/Documents/projects/data-reviews/test_code.py", line 52, in

total_reviews = data.get(‘data’, {}).get(‘node’, {}).get(‘reviews’, {}).get(‘aggregates’, {}).get(‘count’, {})
AttributeError: ‘NoneType’ object has no attribute ‘get’

I’m not what to do here or it try: except: the only option?

import json

resp = """{
    "data": {
        "node": {
            "myReviews": {
                "totalReviews": 0
            },
            "allReviews": {
                "aggregates": null
            }
        }
    }
}"""

data = json.loads(resp)

total_ratings = data.get('data', {}).get('node', {}).get('myReviews', {}).get('totalReviews', 0)
total_reviews = data.get('data', {}).get('node', {}).get('allReviews', {}).get('aggregates', {}).get('count', 0)

2

Answers


  1. The fallback argument of the get method only gets used if the value is not defined. If get explicitly returns None then that is the result.

    Like you write, the common workaround is to use try/except:

    try:
        total_ratings = data['data']['node']['myReviews']['totalReviews']
    except KeyError:
        total_ratings = 0
    try:
        total_reviews = data['data']['node']['allReviews']['aggregates']['count']
    except KeyError:
        total_reviews = 0
    
    Login or Signup to reply.
  2. You can use the optional object_hook function to replace the null value.

    import json
    
    
    def as_null(resp):
        if "aggregates" in resp and resp["aggregates"] is None:
            print(resp)
            resp["aggregates"] = {'count': 0}
            print(resp)
        return resp
    
    
    resp = """{
        "data": {
            "node": {
                "myReviews": {
                    "totalReviews": 0
                    
                    
                },
                "allReviews": {
                    "aggregates": null
                }
            }
        }
    }"""
    
    data = json.loads(resp, object_hook=as_null)
    total_ratings = data.get('data', {}).get('node', {}).get('myReviews', {}).get('aggregates', {}).get('count', 0)
    total_reviews = data.get('data', {}).get('node', {}).get('allReviews', {}).get('aggregates', {}).get('count', 0)
    print(data)
    print(total_ratings, total_ratings)
    
    ----------------------
    
    {'aggregates': None}
    {'aggregates': {'count': 0}}
    {'data': {'node': {'myReviews': {'totalReviews': 0}, 'allReviews': {'aggregates': {'count': 0}}}}}
    0 0
    

    Or use it to immediately get the count values you need.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search