Single quotes are not valid JSON: How to Fix It in PHP
PHP json_decode fails on single-quoted keys and values because JSON only allows double quotes. See the causes, the PHP fix steps and a safe decoding…
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →JSON strings and keys must be wrapped in double quotes, so a single quote makes json_decode fail. PHP reports it as a generic syntax error, and the fix is to switch every JSON quote to a double quote.
What “Single quotes are not valid JSON” means
The parser found a single quote where JSON requires a double quote. The JSON standard defines strings, including object keys, as text between double quotation marks. PHP's json_decode does not accept single quotes. It returns null and json_last_error_msg() returns the text Syntax error, not a message that names the quote.
Causes and the fix for each
Object keys are wrapped in single quotes.
Keys are strings, so they need double quotes like any other string. Single-quoted keys are the most common trigger.
Before (fails):{'name': "Ada"}
After (valid):
{"name": "Ada"}
String values are wrapped in single quotes.
The keys may be correct while a value still uses single quotes. The parser stops at the first one it meets.
Before (fails):{"name": 'Ada'}
After (valid):
{"name": "Ada"}
A Python dict repr was pasted in as JSON.
Python prints dicts with single quotes and writes True and None. JSON needs double quotes, true and null, so all three have to change.
Before (fails):{'ok': True, 'items': None}
After (valid):
{"ok": true, "items": null}
One hand-edited value broke a file that was valid before.
A single value typed with single quotes makes json_decode fail on the entire document. PHP does not skip the bad value and keep the rest. It returns null and sets a syntax error. That is why a config file that worked yesterday can fail today after a one-line manual edit.
Before (fails):{"name": "Ada", "role": 'admin'}
After (valid):
{"name": "Ada", "role": "admin"}
A JavaScript object literal was copied from code or a console.
JavaScript allows unquoted keys and single-quoted strings in object literals. JSON allows neither.
Before (fails):{name: 'Ada', age: 36}
After (valid):
{"name": "Ada", "age": 36}
How to fix it in PHP
- Call json_last_error_msg() after json_decode and log it together with the raw input. A single quote shows up as Syntax error, so the raw text is what tells you the cause.
- Look at the first few characters of the raw string. If it starts with {' or ' the quotes are the problem.
- Do not run str_replace to turn every single quote into a double quote. Apostrophes inside text such as it's would break, and quotes inside values would end up unescaped.
- Fix the producer. Build the data as a PHP array and call json_encode, which writes double quotes for you. Do not assemble JSON by string concatenation.
- For a one-off payload, paste it into [the Repair tool and copy the corrected output. It converts the quotes and fixes related problems in the same pass.
- Run the result through the Validator before you commit it or send it on.
<?php
$raw = file_get_contents('payload.json');
try {
$data = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
error_log('Bad JSON: ' . $e->getMessage() . ' | starts with: ' . substr($raw, 0, 40));
throw new InvalidArgumentException('Payload is not valid JSON. Check for single quotes.', 0, $e);
}
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Syntax error | json_last_error_msg() text for single quotes and most other malformed input. | Inspect the raw string and replace single quotes with double quotes. |
| JSON_ERROR_SYNTAX | The error constant behind the Syntax error message. | Check the raw input, then fix the producer. |
| JsonException: Syntax error | Thrown by json_decode when JSON_THROW_ON_ERROR is set. | Catch the exception and log the start of the input. |
| Unexpected token ' in JSON at position 1 | The same single-quote mistake, as JavaScript reports it. | Use double quotes for keys and values. |
| Expecting property name enclosed in double quotes | The same single-quote mistake on a key, as Python reports it. | Wrap the key in double quotes. |
Related errors
- Invalid JSON Meaning: What It Is and How to Fix It
- Expecting property name enclosed in double quotes (Python Fix)
- JavaScript JSON.parse() Errors: What They Mean and How to Fix Them
- Trailing comma in JSON: How to Fix It in PHP
- Invalid escape sequence in JSON: How to Fix It in PHP
Key takeaways
- JSON strings and keys need double quotes.
- PHP reports single quotes as Syntax error, not by name.
- Do not fix quotes with a blind str_replace.
- Produce JSON with json_encode instead of building strings by hand.
- Use JSON_THROW_ON_ERROR and log the start of the raw input.
FAQ
Why does PHP only say Syntax error?
json_decode reports one general error code for most malformed input. It does not name the character or the position, so you have to check the raw string yourself.
Can json_decode accept single quotes with a flag?
No. None of the json_decode flags relax the quote rule. The input has to be valid JSON before you decode it.
Is a single quote inside a double-quoted string a problem?
No. An apostrophe inside a double-quoted string is a normal character. Only single quotes used as string delimiters cause the error.
Why does var_export output fail in json_decode?
var_export writes PHP array syntax with single quotes and => arrows. That is PHP code, not JSON. Use json_encode to produce JSON.
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 →The JSON Medic newsletter
One JSON error and its fix, weekly, plus new guides and tools. Free.
We send a confirmation link first. Unsubscribe with one click in any mail. See the privacy policy.
Related Articles