ToErotimatiko
ToErotimatiko

Reputation: 189

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 ?

Upvotes: 3

Views: 6647

Answers (4)

Shweta Chandel
Shweta Chandel

Reputation: 895

Access dict keys correctly.

mastercounter = 0
phdcounter = 0
nodeegreecounter = 0

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

Upvotes: 1

Sergey
Sergey

Reputation: 532

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

Upvotes: 2

autom99
autom99

Reputation: 96

Kindly refer a below URL for all details for your question and answer:

https://www.calhoun.io/how-to-determine-if-a-json-key-has-been-set-to-null-or-not-provided/

Hope it's useful..

Thank you..Enjoy coding.

Upvotes: 0

tkrishtop
tkrishtop

Reputation: 816

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

Upvotes: 2

Related Questions