Remove Null Values from JSON in JavaScript and Python
A JSON.stringify replacer removes most nulls but not all of them. Here is a tested recursive function for JavaScript and Python, and the one case where removing nulls breaks things.
To remove null values from JSON in code, a short recursive function does the job. The Remove Nulls from JSON tool cleans a document in the browser, and its page explains when dropping nulls changes what the data means. When the cleanup has to happen in code, for example before saving a document or sending it to another service, a few lines are enough.
This guide shows the quick option, its gaps, and a recursive function for JavaScript and Python that handles nested objects and arrays the way you choose.
The quick option: a JSON.stringify replacer
A replacer that returns undefined for null drops the property at every depth while serialising:
JSON.stringify(
{ name: "Ada", phone: null, tags: [null, "vip"], address: { line2: null } },
(key, value) => (value === null ? undefined : value),
);
// '{"name":"Ada","tags":[null,"vip"],"address":{}}'Two things are left over. The null inside tags stays, because JSON.stringify writes undefined in an array as null to keep positions. And address remains as an empty object. It also only works at the moment of serialising; you do not get a cleaned object back to use in code.
Remove null values in JavaScript
const isPlainObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
function removeNulls(value) {
if (Array.isArray(value)) return value.map(removeNulls);
if (isPlainObject(value)) {
return Object.fromEntries(
Object.entries(value)
.filter(([, v]) => v !== null)
.map(([k, v]) => [k, removeNulls(v)]),
);
}
return value;
}
removeNulls({ name: "Ada", phone: null, tags: [null, "vip"], address: { line2: null, city: "Oslo" } });
// { name: "Ada", tags: [null, "vip"], address: { city: "Oslo" } }It returns a new object and leaves the input unchanged. Arrays keep their nulls on purpose: in a list such as [score1, null, score3], the position carries meaning, and removing the null would shift the third score into second place. If your arrays are plain lists where position does not matter, change the first line to value.filter((v) => v !== null).map(removeNulls).
Remove null values in Python
def remove_nulls(value):
if isinstance(value, list):
return [remove_nulls(v) for v in value]
if isinstance(value, dict):
return {k: remove_nulls(v) for k, v in value.items() if v is not None}
return value
remove_nulls({"name": "Ada", "phone": None, "tags": [None, "vip"], "address": {"line2": None, "city": "Oslo"}})
# {'name': 'Ada', 'tags': [None, 'vip'], 'address': {'city': 'Oslo'}}A one-level comprehension such as {k: v for k, v in data.items() if v is not None} is common in answers online, but it only cleans the top level. A null inside a nested object stays.
Objects that become empty
Removing nulls can leave an object with nothing in it, such as address: {} when every field was null. Both functions keep it, because an empty object and a missing key are different answers for many readers. If you want those removed as well, that is a different operation, and the Remove Empty Values tool covers it.
When a null has to stay
In a partial update, null often means clear this field. In JSON Merge Patch, {"phone": null} deletes the phone number, while leaving phone out means do not touch it. Run removeNulls on that request and the delete becomes a request that changes nothing.
So remove nulls from data you store or display, not from update requests. If the JSON comes from your own code, it is often simpler to configure the serializer to skip nulls when writing; most serialisation libraries have such an option.
Related tools
Remove empty strings, arrays, objects, and nulls throughout JSON.
Pretty-print JSON with configurable indentation and instant validation.
Deep-merge two or more JSON objects into one.
Strip whitespace to shrink JSON to the smallest valid payload.
Related guides
Leaving out empty arrays saves a few bytes and costs every client a defensive check. Here is how to decide what an API should return, and how to write it into the contract.
Two formatters can disagree about the same JSON file. Here is how a team picks one, applies it in every editor and before every commit, and makes CI enforce it.
The built-in ways to combine objects lose nested settings. Here is a short, tested deep merge for JavaScript and Python, and how to handle arrays, null and __proto__.
Whitespace in JSON is nearly free once gzip runs. Here is where JSON travels without compression, with the extra bytes measured on sample payloads.