Invalid JSON: JavaScript JSON.parse() Fails on NaN and Infinity
JSON.parse() in JavaScript throws a SyntaxError on NaN, Infinity, -Infinity and undefined. See why the standard rejects them and how to repair the text.
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →JSON.parse() throws a SyntaxError because NaN, Infinity and -Infinity are not valid JSON values. Replace them with null, a number, or a string before parsing.
What “Invalid JSON” means
The text is not valid JSON because it holds a token the JSON grammar does not define. JavaScript has NaN and Infinity as number values, but the JSON standard has no way to write them. A number in JSON is digits with an optional sign, fraction and exponent. JSON.parse() reads NaN as an unknown word and throws a SyntaxError. The exact wording depends on the engine, but the cause is the same.
Causes and the fix for each
A bare NaN where a number should be.
The standard grammar has no NaN literal, so the parser stops at the letter N.
Before (fails):{"ratio": NaN}
After (valid):
{"ratio": null}
A bare Infinity as a value.
Infinity is a JavaScript number, but JSON numbers must be finite digits.
Before (fails):{"max": Infinity}
After (valid):
{"max": null}
A negative infinity value.
-Infinity fails for the same reason as Infinity. The minus sign does not change that.
Before (fails):{"min": -Infinity}
After (valid):
{"min": null}
The word undefined used as a value.
JSON has null but no undefined. The parser fails on the letter u.
Before (fails):{"name": undefined}
After (valid):
{"name": null}
Another tool wrote the file with NaN allowed.
Some serializers, such as Python's json.dumps with default settings, write NaN and Infinity. JavaScript then rejects that output.
Before (fails):{"scores": [1.5, NaN, Infinity]}
After (valid):
{"scores": [1.5, null, null]}
How to fix it in JavaScript
- Read the error message and find the token it names. V8 reports the first bad character, such as N, I or u, and often shows the start of the text.
- Paste the text into the Validator to see the exact line and column of the bad value.
- Decide what each bad value should mean. Use null for missing data, a large finite number for a limit, or a string such as "Infinity" if the reader can handle it.
- Fix the producer if you control it. In JavaScript, JSON.stringify() already turns NaN and Infinity into null, so the bad text usually comes from another tool or from hand-built strings.
- If you cannot change the source, run the text through the Repair tool, or use a replacer in code before calling JSON.parse().
- Run the Formatter on the result to confirm the document is valid and readable.
function parseLoose(text) {
try {
return JSON.parse(text);
} catch (err) {
// Match whole strings first so their contents stay untouched.
const fixed = text.replace(
/"(?:[^"\\]|\\.)*"|-?Infinity|NaN|undefined/g,
(m) => (m[0] === '"' ? m : 'null')
);
try {
return JSON.parse(fixed);
} catch (err2) {
throw new Error('Invalid JSON after repair: ' + err2.message);
}
}
}
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Unexpected token 'N', "{"a": NaN}" is not valid JSON | V8 (Chrome, Node.js) found NaN where a value should be. | Replace NaN with null or a number. |
| Unexpected token N in JSON at position 6 | Older V8 wording for the same NaN problem. | Replace NaN with null or a number. |
| Unexpected token 'I', ... is not valid JSON | Infinity or -Infinity used as a value. | Replace it with null or a finite number. |
| Unexpected token 'u', ... is not valid JSON | The word undefined was written into the text. | Use null or remove the key. |
| JSON.parse: unexpected character at line 1 column 7 of the JSON data | Firefox reports the position of the bad token. | Go to that column and fix the value. |
| JSON Parse error: Unexpected identifier "NaN" | Safari names the bad word directly. | Replace NaN with null or a number. |
Related errors
- JavaScript JSON.parse() Errors: What They Mean and How to Fix Them
- Invalid JSON Meaning: What It Is and How to Fix It
- Fix JSON SyntaxError: Unexpected Token
- Fix Python JSON Errors: json.loads() Decode Errors and How to Repair Them
- JSON Trailing Comma Error: Why It Happens and How to Fix It
Key takeaways
- NaN, Infinity, -Infinity and undefined are not valid JSON values.
- JSON.parse() throws a SyntaxError on the first one it reads.
- JSON.stringify() writes null for these values, so the bad text often comes from elsewhere.
- Replace the tokens with null or a finite number before parsing.
- Use the Validator to find the position and the Repair tool to fix the text.
FAQ
Does JSON.stringify() write NaN or Infinity?
No. JSON.stringify() writes null for NaN, Infinity and -Infinity. The bad text usually comes from another serializer or from string concatenation.
Is NaN valid in any JSON standard?
RFC 8259 does not allow NaN or Infinity. Some libraries accept them as an extension, but a strict parser such as JSON.parse() rejects them.
Should I replace NaN with null or 0?
Use null when the value is unknown, since 0 looks like real data. Pick 0 only if your consumer treats it as a valid default.
Why does Python output work in Python but fail in JavaScript?
Python's json module writes NaN and Infinity by default and reads them back. JavaScript's JSON.parse() does not accept them.
Can a reviver function fix NaN?
No. The reviver runs after parsing succeeds, and the parse fails first. The text must be fixed before JSON.parse() runs.
Still dealing with broken JSON?
Paste it in and get it fixed in under 1 second — free, no signup, no install. Works with ChatGPT, Claude, n8n, and any AI output.
Fix My JSON Free →Related Articles