Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - How to check if a key has null value in a JSON?

In a JSON, I want to check and count who has a degree (the highest level one, and what kind) and who hasn't. While counting and checking the Masters, PhDs and such , works, checking if there is null , doesn't.

Part of the JSON

  "id": 125428,
  "Degree": "Master",

different candidate:

  ""id": 125589,
  "Degree": null,

different candidate:

  "id": 944987,
  "Degree": "PhD"

My relevant code is the following :

mastercounter = 0
phdcounter = 0
nodeegreecounter = 0

for candidate in response["person"]:
    if item["Degree"]:
        if item["Degree"]["key"] == "Master":
            mastercounter = mastercounter + 1
        if item["license"]["key"] == "PhD":
            phdcounter = phdcounter + 1
   if item["Degree"] == None: 
       nodegreecounter = nodegreecounter + 1

The error I'm getting is "TypeError: 'NoneType' object is not subscriptable"

Is it the identation wrong, or the code/my whole logic ?

like image 748
ToErotimatiko Avatar asked Mar 05 '26 12:03

ToErotimatiko


2 Answers

What about using if/else?

mastercounter = 0
phdcounter = 0
nodeegreecounter = 0

for candidate in response["person"]:
    if item["Degree"]:
        if item["Degree"]["key"] == "Master":
            mastercounter = mastercounter + 1
        if item["license"]["key"] == "PhD":
            phdcounter = phdcounter + 1
    else: 
       nodegreecounter = nodegreecounter + 1
like image 161
tkrishtop Avatar answered Mar 08 '26 01:03

tkrishtop


It depends on how your JSON is organized. I suspect the response is a sequence of persons. Like this:

response = [{"id": "1", "Degree": "Master"}, {"id": "2", "Degree": null}]

Therefore you should use:

for person in response:

you only need one attribute of a person (named "Degree").

Thus, if a person is a sequence of attributes, the code becomes:

for person in response:
    if person["Degree"] is None:
        nodegreecounter = nodegreecounter + 1
    elif person["Degree"] == "Master":
        mastercounter = mastercounter + 1
    elif person["Degree"] == "PhD":
        phdcounter = phdcounter + 1

If your JSON is organized differently, you should explain the JSON structure before asking for advice.

If your JSON looks like this:

{"key11": {"id": "1", "Degree": "Master"}, "key12": {"id": "2", "Degree": null}}

The code can be:

for key in response:
    if response[key]["Degree"] is None:
        nodegreecounter = nodegreecounter + 1
    elif response[key]["Degree"] == "Master":
        mastercounter = mastercounter + 1
    elif response[key]["Degree"] == "PhD":
        phdcounter = phdcounter + 1

or

for key, person in response.items():
    if person["Degree"] is None:
        nodegreecounter = nodegreecounter + 1
    elif person["Degree"] == "Master":
        mastercounter = mastercounter + 1
    elif person["Degree"] == "PhD":
        phdcounter = phdcounter + 1
like image 27
Sergey Avatar answered Mar 08 '26 00:03

Sergey