undefined Is Not Valid in JSON – How to Handle It

JSON defines six value types: string, number, object, array, boolean (true/false), and null. undefined is not on that list. It is a JavaScript concept that has no equivalent in the JSON specification. This causes several subtle behaviors worth understanding before you build an API or serialize data.

What JSON.stringify does with undefined

When you serialize a JavaScript object using JSON.stringify(), properties with an undefined value are silently dropped from the output:

const user = {
  name: "Alice",
  email: undefined,   // will be dropped
  age: 30
};

console.log(JSON.stringify(user));
// Output: {"name":"Alice","age":30}
// "email" is gone entirely

This is often surprising. The object had three properties but the JSON has two — and no error was thrown. If the receiver expects an email key, it will get undefined when it reads data.email in JavaScript, or an equivalent missing-key behavior in other languages.

undefined in arrays becomes null

Arrays behave differently. Because dropping an array element would change the array's length, JSON.stringify() converts undefined array elements to null instead:

JSON.stringify([1, undefined, 3]);
// Output: "[1,null,3]"

A top-level undefined returns undefined from stringify

JSON.stringify(undefined);
// Output: undefined (not a string — the function returns the JS value undefined)

null vs undefined in JSON

null is valid JSON. Use it when you want to explicitly represent the absence of a value in a way that survives serialization:

const user = {
  name: "Alice",
  email: null,  // explicitly absent — survives JSON.stringify
  age: 30
};

console.log(JSON.stringify(user));
// Output: {"name":"Alice","email":null,"age":30}

If you are designing an API, use null for optional fields that have no current value. This makes the structure predictable — consumers always receive the key and know it is intentionally absent.

How to handle undefined before serializing

If you receive data that might contain undefined values and need to serialize it safely, you have a few options:

Option 1 — replace undefined with null explicitly

const clean = Object.fromEntries(
  Object.entries(obj).map(([k, v]) => [k, v === undefined ? null : v])
);

Option 2 — use the JSON.stringify replacer function

const json = JSON.stringify(obj, (key, value) =>
  value === undefined ? null : value
);

Option 3 — remove undefined properties intentionally

If omitting the key is the correct behavior, rely on JSON.stringify()'s default behavior — it will drop undefined properties automatically. This is correct when the key is genuinely optional and receivers are expected to use a default when it is missing.

Parsing JSON with undefined

You cannot include the literal text undefined in a JSON string and expect it to parse:

JSON.parse('{"name": undefined}');
// SyntaxError: Unexpected token u in JSON at position 9

If you see this error, the data was not properly serialized. The source should have used null instead of undefined, or omitted the key entirely.

Related articles