Single quotes are not valid JSON: Fix JSON.parse() in JavaScript
JSON only allows double quotes around strings and keys. Here is why JSON.parse() rejects single quotes in JavaScript, and how to fix it.
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →JSON requires double quotes around every string and every object key. JSON.parse() throws a SyntaxError when it finds a single quote where a double quote should be.
What “Single quotes are not valid JSON” means
The error means the parser found a single quote where JSON allows only a double quote. The JSON standard defines a string as characters wrapped in double quotation marks. Single quotes are not part of the grammar. JavaScript object literals accept them, so the mistake is easy to make. The parser stops at the first single quote and reports an unexpected token or a similar message, depending on the engine.
Causes and the fix for each
Object keys are wrapped in single quotes.
JSON keys are strings, so they need double quotes. A key in single quotes is rejected at the first quote character. JavaScript object literals allow this form, which is why it slips into JSON text copied from code.
Before (fails):{'name': "Ada"}
After (valid):
{"name": "Ada"}
String values are wrapped in single quotes.
Values follow the same rule as keys. A string value must start and end with a double quote. Mixing a valid key with a single-quoted value still fails at the value.
Before (fails):{"name": 'Ada'}
After (valid):
{"name": "Ada"}
Array items are wrapped in single quotes.
Strings inside arrays follow the same rule as strings in objects. Each item that is a string needs double quotes on both sides. A list copied from a JavaScript array literal often carries single quotes along with it.
Before (fails):{"tags": ['a', 'b']}
After (valid):
{"tags": ["a", "b"]}
A JavaScript object was turned into text with String() or a template literal.
String(obj) gives [object Object], and util.inspect or console.log output uses single quotes. Neither is JSON. JSON.stringify() is the call that writes valid JSON with double quotes.
Before (fails):{'id': 1, 'ok': true}
After (valid):
{"id": 1, "ok": true}
A Python repr or a hand-built string was sent as the response body.
Python prints dicts with single quotes when you call str() on them. If a server returns that text, JSON.parse() fails on it. The fix belongs on the server, or in a repair step on your side.
Before (fails):{'active': True}
After (valid):
{"active": true}
How to fix it in JavaScript
- Read the error and find the position. Most engines print the character offset or the unexpected token, which points at the first single quote.
- Check where the text came from. If your own code built it, replace manual string building with JSON.stringify(value).
- If the text comes from another service, ask for a fix at the source. Python str() output and console logs are not JSON.
- For a one-off payload, paste it into the Repair tool. It converts single quotes to double quotes and shows the valid result.
- Run the result through the Validator to confirm it parses before you use it in code.
- For a literal you control, write the JSON with double quotes by hand, or use the Formatter to clean it up.
function parseLoose(text) {
try {
return JSON.parse(text);
} catch (err) {
// Last resort for simple cases only.
// This breaks on apostrophes inside strings.
const swapped = text
.replace(/'/g, '"')
.replace(/\bTrue\b/g, 'true')
.replace(/\bFalse\b/g, 'false')
.replace(/\bNone\b/g, 'null');
try {
return JSON.parse(swapped);
} catch (err2) {
throw new Error('Not valid JSON: ' + err.message);
}
}
}
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Unexpected token ' in JSON at position 2 | Older V8 wording for a single-quoted key at the start of an object. | Change the quotes to double quotes. |
| Expected property name or '}' in JSON at position 1 | Newer V8 wording when a key uses single quotes. | Use double quotes around the key. |
| JSON.parse: expected property name or '}' | Firefox wording for a single-quoted key. | Use double quotes around the key. |
| JSON.parse: unexpected character at line 1 column 2 | Firefox wording when the first character is a single quote. | Replace single quotes with double quotes. |
| Unexpected token ' in JSON at position 0 | The whole text is a single-quoted string. | Wrap the text in double quotes and escape inner quotes. |
| Unexpected token o in JSON at position 1 | An object was passed to JSON.parse and became [object Object]. | Skip JSON.parse for values that are already objects. |
Related errors
- JavaScript JSON.parse() Errors: What They Mean and How to Fix Them
- Single quotes are not valid JSON: Fixing json.loads() in Python
- Invalid JSON Meaning: What It Is and How to Fix It
- Trailing comma in JSON: How to Fix It in JavaScript
- Unterminated string in JSON: Fix JSON.parse() in JavaScript
Key takeaways
- JSON strings and keys need double quotes.
- Single quotes are valid in JavaScript literals but not in JSON.
- Use JSON.stringify() to produce JSON from objects.
- A blind quote swap fails on apostrophes inside strings.
- Use the Repair tool for a quick fix and the Validator to confirm it.
FAQ
Can JSON.parse() ever accept single quotes?
No. JSON.parse() follows the JSON grammar, which allows only double quotes around strings. There is no option to change that. JSON5 allows single quotes, but it needs a separate parser.
Why does my JavaScript object work with single quotes but JSON does not?
JavaScript object literals are part of the language syntax and accept single quotes, double quotes and backticks. JSON is a separate text format with a stricter grammar.
Is a global replace of ' with " safe?
Only for simple data. It breaks any string that contains an apostrophe, such as it's or O'Brien. A repair tool or a real parser handles those cases better.
How do I write valid JSON from a JavaScript object?
Call JSON.stringify(value). It writes double quotes around strings and keys, and it escapes characters that need escaping.
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