Skip to content
LocalOnly
JSON Tools

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.

Updated October 18, 20263 min readTool runs in your browser

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.

Frequently asked questions

Command Palette

Search for a tool or command