pythonjsonformatter

Missing, null, and empty JSON values in API responses

A missing property and a property set to null are different data. Avoid defaulting them into the same value before you inspect the response.

Start with the value and the property itself

Imagine a profile response with an optional nickname. All of these are valid JSON objects, but they communicate different states:

DocumentWhat is present
{}No nickname property
{"nickname":null}A property with a null value
{"nickname":""}A property containing an empty string
{"nickname":"Ada"}A property containing text

Whether omission means “unknown”, “not requested”, or “unchanged” depends on the API contract. JSON defines the syntax and values, not your application's meaning. Do not infer a deletion instruction just because you saw null in an arbitrary response.

Do not let dict.get() erase the distinction

In Python, json.loads() converts JSON null to None. Using data.get("nickname") also returns None when the property is absent, unless you provide another default. Membership makes the distinction explicit:

import json

for raw in ('{}', '{"nickname":null}', '{"nickname":""}'):
    data = json.loads(raw)
    if "nickname" not in data:
        state = "missing"
    elif data["nickname"] is None:
        state = "null"
    elif data["nickname"] == "":
        state = "empty string"
    else:
        state = "value"
    print(state)

Output:

missing
null
empty string

This example assumes the top-level value is an object. Check that assumption before applying the same logic to an arbitrary JSON response. For nested data, inspect each intermediate object rather than treating a missing parent as an empty child.

Zero and false are values too

A shortcut such as value or "default" also replaces zero, false, an empty list, and an empty string. That can turn a valid zero quantity into a fallback quantity or overwrite an explicit disabled flag. Default only the states your schema calls absent.

import json

data = json.loads('{"quantity":0,"enabled":false,"labels":[]}')
quantity = data["quantity"] if "quantity" in data else 1
print(quantity)
print(data["enabled"] is False)
print(len(data["labels"]))

Output:

0
True
0

An empty array says there are currently no listed items. A missing array property may instead mean that items were not included. Validate the response against its documented schema before flattening those states for display.

Review an API change before normalizing it

Paste {} as the original and {"nickname":null} as the updated document in Diff Checker. It reports $.nickname as an added property. Comparing null with an empty string reports a changed value. If you fill absent properties with null before comparing, you erase the first difference yourself.

Open a payload with explicit empty values to inspect how null, false, zero, an empty string, and an empty array remain distinct. The converter cannot decide what each state means for your API. See the JSON value definitions for the underlying types.

By Python JSON Formatter ·