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
Answer from tkrishtop on Stack Overflow
Top answer
1 of 4
2

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
2 of 4
2

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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ check-if-json-object-has-empty-value-in-python
Check if Json Object has Empty Value in Python - GeeksforGeeks
July 23, 2025 - import json json_data = '{"name": "John", "age": 25, "city": "", "email": null}' parsed_json = json.loads(json_data) for key, value in parsed_json.items(): if not value: print(f"Empty value found for key: {key}") ... This Python code uses the json module to parse a JSON string (json_data) into a Python dictionary (parsed_json).
Discussions

How to parse completely empty JSON key/values?
I'm not sure why you would want that, or what the logic is here. Why does an empty dict map to an empty string? How are you selecting the values to fetch for the other dicts? Are all the dicts guaranteed to either be empty or have a single key only? More on reddit.com
๐ŸŒ r/learnpython
7
3
November 18, 2022
python - Checking if JSON key is empty - Stack Overflow
0 Send nested JSON data to Postgres with Python - canยดt find a way to insert null values on a table with psycopg2 More on stackoverflow.com
๐ŸŒ stackoverflow.com
Conditional statement to check if a JSON key is null with Python 3 - Stack Overflow
This has been updated to clarify the question so it can better help others in the future. I am attempting to use an if statement to test if the key classes exists in a JSON structure using Python. to More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 3, 2018
How to check if something is in a JSON object before running if statement
There's no such thing as a "JSON object." There's a JSON string, which represents an object, but once you've deserialized the string you're holding a regular Python dictionary or list. So all of the regular Python membership tests work - you test whether the collection contains a key or a value by using in. The issue that I have run into so that sometimes those attributes (like subtitle) don't exist in the JSON because they do not exist in the file. if "subtitle" not in record or record["subtitle"] != "French": add_french_subtitles(record) # or whatever More on reddit.com
๐ŸŒ r/learnpython
11
6
September 15, 2023
๐ŸŒ
Processing
processing.org โ€บ reference โ€บ JSONObject_isNull_.html
isNull() / Reference / Processing.org - JSONObject
January 1, 2021 - ... JSONObject json; void setup() ... json.setString("species", null); if (json.isNull("species") == true) { println("The species is undefined"); } else { println("The ID is " + json.getString("species")); } }...
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ json โ€บ python check if key exists in json and iterate the json array
Python Check if key exists in JSON and iterate the JSON array
May 14, 2021 - As you know, the json.loads method converts JSON data into Python dict so we can use the get method of dict class to assign a default value to the key if the value is missing. import json studentJson ="""{ "id": 1, "name": "john wick", "class": null, "percentage": 75, "email": "jhon@pynative.com" ...
๐ŸŒ
Like Geeks
likegeeks.com โ€บ home โ€บ python โ€บ how to handle json missing keys in python
How To Handle JSON missing keys in Python
January 27, 2024 - Sometimes, keys might be present, but their values are null (None in Python). Hereโ€™s a method to handle such cases: customer_data = { "name": "Alex", "age": 28, "email": None } email = customer_data.get("email") if email is None: email = "No email provided" print(email) ... Here, the code checks if the โ€™emailโ€™ key exists but has a null value, then provides a default message. The same way when you deal with nested JSON objects, using get with default values can prevent errors due to missing nested keys.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to check if something is in a json object before running if statement
r/learnpython on Reddit: How to check if something is in a JSON object before running if statement
September 15, 2023 -

Ok... This is a little complicated... So... Sorry in advance

I have a function that returns attributes of a video file (FFprobe) in a JSON object, and then a little factory that parses the JSON looking for specific attributes, and running if statements on those attributes.

I.E. One of attributes in the JSON is subtitle format. So if the subtitle format != a desired format, then set a variable that is used in another format for encoding the subtitle to the desired format

The issue that I have run into so that sometimes those attributes (like subtitle) don't exist in the JSON because they do not exist in the file.

So I sort of need to check if the attribute in the JSON exists, before I check to see if if that attribute is the desired attribute and start setting variables

How do I do this?

Would it be as simple as:

json_object= json.loads(studentJson)
if "subtitle_format" in json_object: 
    print("Key exist in json_object") 
    print(subtitle_format["ASS"], " is the subtitle format") 
else: 
    print("Key doesn't exist in JSON data")

If yes, would I get yelled at if the if statement had a few layers? Psudo:

if subtitle_format in json_object:
    if subtitle_format == ass
         if subtitle_format == english
             encode 

๐ŸŒ
FreeKB
freekb.net โ€บ Article
Determine if JSON key contains an empty list
#!/usr/bin/python3 import json raw_json = '{ "foo": [] }' try: parsed_json = json.loads ( raw_json ) except Exception as exception: print(f"Got the following exception: {exception}") if len(parsed_json['foo']) == 0: print("The foo key contains an empty list") else: print("The foo key does NOT contain an empty list")
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-35910.html
value null when update in json file
Hi! I have 4 functions, one for adding a new item to the json file, one for building a new dictionary, one for validating the data I enter from the keyboard, and one for updating one or more fields in the existing json file. My problem is when I try...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ check-if-python-json-object-is-empty
Check If Python Json Object is Empty - GeeksforGeeks
April 25, 2024 - Whether you prefer using the len() function, the not operator, or directly comparing the JSON object with an empty dictionary, you have multiple options to accomplish this task efficiently.