SmartJson.io
← All JSON error messages

"undefined" is not valid JSON: JSON.parse was given undefined

By Santhi · Messages captured October 9, 2026 on 11 runtimes · How we test

JSON.parse turns its argument into a string before reading it. undefined becomes the nine letters "undefined", which is not JSON. So the message almost always means the variable you parsed was never set, not that a server sent the word undefined.

What each runtime prints

We ran these inputs through JSON.parse in each browser and Node version, and through json.loads in Python. These are the messages exactly as printed.

Input: JSON.parse(undefined)
undefined
RuntimeExact message
Chrome 151Node 24Node 22Node 20"undefined" is not valid JSON
Firefox 153JSON.parse: unexpected character at line 1 column 1 of the JSON data
Safari 26 (WebKit)BunJSON Parse error: Unexpected identifier "undefined"
Node 18Unexpected token u in JSON at position 0
Python 3.13Python 3.12Python 3.10JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Input: undefined as a value
{"a": undefined}
RuntimeExact message
Chrome 151Node 24Node 22Node 20Unexpected token 'u', "{"a": undefined}" is not valid JSON
Firefox 153JSON.parse: unexpected character at line 1 column 7 of the JSON data
Safari 26 (WebKit)BunJSON Parse error: Unexpected identifier "undefined"
Node 18Unexpected token u in JSON at position 6
Python 3.13Python 3.12Python 3.10JSONDecodeError: Expecting value: line 1 column 7 (char 6)
  • Chrome and Node 20+ put the word itself in quotes, "undefined" is not valid JSON, when the whole argument was undefined. When the word appears inside an object, they say "Unexpected token 'u'" and quote the whole text instead.
  • Firefox says "unexpected character at line 1 column 1", the same message it gives for an HTML page or a byte order mark.
  • Safari and Bun name the word: Unexpected identifier "undefined".
  • Python has no undefined. It gets the text from a JavaScript client and says "Expecting value", at column 1 for the bare word and at the start of the value inside an object.
  • JSON.parse(null) does not throw. String(null) is "null", which is valid JSON, so it returns null. That is why localStorage.getItem() for a missing key is safe and a missing property is not.

Edit it or paste your own. It runs in your browser; nothing is uploaded.

Open the full JSON Repair tool →

Common causes

  1. 1

    A property or variable that does not exist

    JSON.parse(data.payload) when the field is called body, a function that forgot to return, or an environment variable that is not set (process.env.CONFIG is undefined, not an empty string).

  2. 2

    Reading storage as a property

    localStorage.getItem('user') returns null for a missing key, and JSON.parse(null) is null. localStorage.user returns undefined for the same missing key, and JSON.parse throws.

  3. 3

    Storing JSON.stringify(undefined)

    JSON.stringify(undefined) returns undefined, not a string. localStorage.setItem() then stores the text "undefined", and every later read of that key fails with this error until it is removed.

  4. 4

    JSON built with a template string

    A body like `{"id": ${id}}` with an unset id becomes {"id": undefined}. JSON.stringify never writes undefined: it drops object keys whose value is undefined and writes null inside arrays.

How to fix it

Treat "no value" as its own case before parsing, and build JSON with JSON.stringify so undefined can never end up in the text.

JavaScript

function parseOrNull(text) {
  if (text === undefined || text === null || text === '') return null;
  return JSON.parse(text);
}

const user = parseOrNull(localStorage.getItem('user'));

// Clear a key that holds the text "undefined" from an earlier bug
if (localStorage.getItem('user') === 'undefined') localStorage.removeItem('user');

Python

import json

raw = request.get_data(as_text=True)
# A JavaScript client sent String(undefined) or JSON.stringify(undefined)
data = None if raw.strip() in ("", "undefined") else json.loads(raw)
  • If you see quotes around undefined in the Chrome or Node message, look at the variable you passed, not the server. Log typeof value just before the parse.
  • JSON Repair turns undefined inside an object into null and says so. It refuses the bare word undefined, because there is no data in it to repair.

Related

  • "[object Object]" is not valid JSON
  • JSON Repair
  • JSON Stringify

How we tested: the messages above come from running each input through the real parsers (Chrome 151, Firefox 153, Safari 26 (WebKit), Node 24, Node 22, Node 20, Node 18, Bun, Python 3.13, Python 3.12, Python 3.10) on October 9, 2026. The script and inputs are listed on How we test.

Published October 10, 2026. Found a message that reads differently in your version? Tell us and we will add it.

All Tools & Converters

JSON Tools

  • JSON Hub
  • JSON Formatter
  • JSON Validator
  • JSON Repair
  • JSON Error Messages
  • JSON Viewer
  • JSON Minify
  • JSON Stringify
  • JSON Sorter
  • JSON Compare
  • JSON to CSV
  • JSON to XML
  • JSON to YAML

XML Tools

  • XML Hub
  • XML Formatter
  • XML Validator
  • XML Compare
  • SOAP Formatter
  • WSDL Formatter
  • XML to JSON
  • XML to CSV

CSV Tools

  • CSV Hub
  • CSV Formatter
  • CSV Validator
  • CSV to JSON
  • CSV to XML
  • Compare Tools

    • JSON Compare
    • XML Compare
    • YAML Compare
    • CSV Compare
    • Text Compare

YAML Tools

  • YAML Hub
  • YAML Formatter
  • YAML Validator
  • YAML to JSON
  • Other Tools

    • Converters Hub
SmartJson.io

JSON, XML, CSV and YAML tools that run in your browser. Nothing you paste is uploaded.

Sitemap

Company

  • About Us
  • Contact
  • How We Test
  • Changelog
  • Privacy Policy
  • Terms of Service
  • Cookie Policy
  • Disclaimer

© 2026 SmartJson.io. All rights reserved.

Report a problem with this page
Made withfor developers