jsondecode.com logo

Fix: JSON.stringify() Drops undefined

JSON.stringify() silently removes object keys with undefined values and converts undefined array elements to null. No error is thrown — data disappears without warning.

The Three Behaviors

1. Object Key With undefined Value — Dropped

const user = { name: "Alice", age: undefined, role: "admin" };
JSON.stringify(user);
// => '{"name":"Alice","role":"admin"}'
//    ↑ "age" key is completely gone

2. Array Element undefined — Becomes null

const scores = [10, undefined, 30];
JSON.stringify(scores);
// => '[10,null,30]'
//      ↑ undefined became null, not dropped

3. Function Values — Also Dropped

const obj = { name: "Alice", greet: () => "hi" };
JSON.stringify(obj);
// => '{"name":"Alice"}'
//    ↑ function key "greet" is gone

NaN and Infinity Also Lose Their Values

Unlike undefined, NaN and Infinity keep their keys but become null:

JSON.stringify({ score: NaN, limit: Infinity, neg: -Infinity });
// => '{"score":null,"limit":null,"neg":null}'

Solutions

Replacer Function — Convert undefined to null

Pass a replacer as the second argument to preserve all keys:

const user = { name: "Alice", age: undefined };

JSON.stringify(user, (key, val) => (val === undefined ? null : val));
// => '{"name":"Alice","age":null}'
//    ↑ key preserved, value is null

safeStringify Helper

A reusable utility that also handles NaN and Infinity:

function safeStringify(value: unknown, indent?: number): string {
  return JSON.stringify(
    value,
    (_, val) => {
      if (val === undefined) return null;
      if (typeof val === "number" && !isFinite(val)) return null;
      return val;
    },
    indent
  );
}

safeStringify({ name: "Alice", age: undefined, score: NaN });
// => '{"name":"Alice","age":null,"score":null}'

Quick Reference

Input valueAs object keyIn array
undefinedKey dropped→ null
functionKey dropped→ null
SymbolKey dropped→ null
NaN→ null (key kept)→ null
Infinity / -Infinity→ null (key kept)→ null
null→ null (key kept)→ null
string, number, booleanPreservedPreserved
plain object / arrayPreservedPreserved

Validate Your JSON Below

Paste the output of JSON.stringify() here to inspect what survived serialization.

Input JSON
Formatted Output
Formatted JSON will appear here

Frequently Asked Questions

Why does JSON.stringify() remove keys with undefined values?

The JSON specification has no undefined type — JSON only has string, number, boolean, null, object, and array. When JSON.stringify() encounters an object key whose value is undefined, it omits the key entirely rather than outputting an invalid token. This is by design but can cause silent data loss if you are not expecting it.

What happens to undefined in a JSON array?

Unlike object keys, array slots cannot be removed without changing array indices. So JSON.stringify() converts undefined array elements to null. For example, [1, undefined, 3] becomes [1,null,3]. The same applies to function values and Symbol values in arrays.

Does JSON.stringify() also drop function values?

Yes. Function values on object keys are dropped silently, just like undefined. In arrays they become null. Symbol-keyed properties are also skipped. Only serializable JSON types (string, number, boolean, null, plain objects, arrays) survive JSON.stringify().

What happens to NaN and Infinity in JSON.stringify()?

NaN, Infinity, and -Infinity are all serialized as null by JSON.stringify(). Unlike undefined, they do not cause key omission — the key is preserved but the value becomes null. This is another form of silent data loss to watch out for alongside undefined.

How can I serialize undefined as null instead of dropping the key?

Use the replacer function argument of JSON.stringify(): JSON.stringify(obj, (key, val) => val === undefined ? null : val). This converts every undefined value to null, preserving the key in the output. Alternatively, write a safeStringify helper that pre-processes the object with the same logic.

If jsondecode.com saved you time, share it with your team

Free forever. No ads. No sign-up. Help other developers find it.