Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

SyntaxError: "undefined" is not valid JSON in a React app

ErrorSyntaxError: "undefined" is not valid JSON

Short answer

JSON.parse turns any non-string argument into a string first, so undefined becomes the text undefined, which is not JSON. Find the variable you pass in. It is usually a missing prop, context value or stored item. Check the type before parsing, and never store the result of JSON.stringify(undefined).

  • Applies to React 19, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the stack trace and find the JSON.parse call. Log the value and its type right before it.

    console.log(typeof value, value);
    JSON.parse(value);
  2. 2Match what you see, then jump to its fix

    1. 1typeof value logs undefined, often on the first render or when a context or prop is not set yet.The value reaching JSON.parse is undefinedFix
    2. 2The error appears when parsing a value read back from storage.Something undefined was saved with JSON.stringifyFix

What does the error look like?

  • SyntaxError: "undefined" is not valid JSON

Cause 1 of 2

The value reaching JSON.parse is undefined

1Check

MDN documents that the argument is converted to a string, so undefined is parsed as the four letters undefined. Trace where the variable comes from: a prop, a useContext value or an object field that does not exist.

console.log(typeof value, value);
// for a context: check the Provider wraps this component

2Fix

Parse only when you have a string, and give the value a default.

function safeParse(text, fallback = null) {
  if (typeof text !== 'string') return fallback;
  try {
    return JSON.parse(text);
  } catch (err) {
    if (err instanceof SyntaxError) return fallback;
    throw err;
  }
}

3Verify

Reload with the data missing. The component renders the fallback and the console shows no SyntaxError.

safeParse(undefined) // null

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 2

Something undefined was saved with JSON.stringify

1Check

JSON.stringify(undefined) returns undefined, not a string, and Web Storage holds strings only. So a write like setItem(key, JSON.stringify(state)) is only safe when state is defined.

console.log(JSON.stringify(undefined)); // undefined
console.log(localStorage.getItem('user'));

2Fix

Skip the write when the value is undefined, and remove any bad key already stored.

if (state !== undefined) {
  localStorage.setItem('user', JSON.stringify(state));
} else {
  localStorage.removeItem('user');
}

3Verify

Reload the page. getItem returns null for a missing key, and JSON.parse(null) gives null without an error.

localStorage.getItem('user')

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Wrap every JSON.parse of external data (storage, URL, props) in one helper that checks the type and catches SyntaxError.
  • Give React context a real default value, or throw a clear error when the Provider is missing.
  • Write to storage only after checking the value is not undefined.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.