SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON dataShort answer
The text your script returned is not JSON. Column 1 means it fails at the very first character, so the body usually starts with HTML: a PHP warning, an error page, or stray output before your echo. Read the raw response in the Network tab or with curl, then fix what is printed before the JSON.
Fix it fast
1Find out which cause you have
Look at what the server really sent, not at what the browser tried to parse. The first characters show the cause.
curl -i https://example.com/api/items.php2Match what you see, then jump to its fix
- 1
The body starts with <br /> or <b>Warning</b> or <b>Notice</b>A PHP warning or notice is printed before the JSONFix - 2
The body starts with <!DOCTYPE or <html, or the status is 404 or 500The request got an HTML page, not your script's answerFix - 3
The body starts with a space, a blank line or text from an included fileOutput or whitespace before the echo, or no JSON Content-TypeFix - 4
The body starts with { or [ but the message points to a later character, or you built the JSON by handThe text is not valid JSONFix
- 1
What does the error look like?
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON dataSyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
Cause 1 of 4
A PHP warning or notice is printed before the JSON
1Check
With display_errors on, PHP writes messages into the response, and html_errors adds HTML tags. Those characters come before your JSON.
curl -s https://example.com/api/items.php | head -c 200
php -i | grep -E "^(display_errors|html_errors|log_errors)"2Fix
Fix the warning. Stop errors being printed in responses and log them instead.
; php.ini
display_errors = Off
html_errors = Off
log_errors = On
error_log = /var/log/php/errors.log3Verify
The first byte of the response is now { or [.
curl -s https://example.com/api/items.php | head -c 1Confirmed by php.net
Cause 2 of 4
The request got an HTML page, not your script's answer
1Check
response.json() does not look at the status code or the content type. A 200 reply with HTML throws a SyntaxError, and a wrong URL returns the site's error page.
curl -s -o /dev/null -w "%{http_code} %{content_type}\n" https://example.com/api/items.php2Fix
Correct the URL, then check response.ok and the content type before parsing.
const response = await fetch('/api/items.php');
if (!response.ok) {
throw new Error('HTTP ' + response.status);
}
const data = await response.json();3Verify
A bad URL now fails with a clear HTTP status message, a good URL returns data.
curl -i https://example.com/api/items.php | head -n 1Confirmed by developer.mozilla.org
Cause 3 of 4
Output or whitespace before the echo, or no JSON Content-Type
1Check
Anything outside the PHP tags is output, and so is anything printed by an included file. The manual notes blank lines in a file count as output. Print the first bytes visibly.
curl -s https://example.com/api/items.php | head -c 20 | cat -A2Fix
Remove the stray output. Send the JSON header before any output, then echo only the encoded data.
<?php
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['status' => 'ok']);3Verify
The response header says application/json and the body begins with the JSON.
curl -i https://example.com/api/items.php | head -n 12Confirmed by php.net
Cause 4 of 4
The text is not valid JSON
1Check
JSON allows no trailing commas, no single-quoted names and no leading zeros in numbers.
curl -s https://example.com/api/items.php | python3 -m json.tool2Fix
Build the reply with json_encode() on a PHP array instead of joining strings, or fix the syntax shown by the checker.
<?php
echo json_encode(['items' => $rows]);3Verify
The checker prints the formatted JSON without an error.
curl -s https://example.com/api/items.php | python3 -m json.toolConfirmed by developer.mozilla.org
How do I stop it coming back?
- Turn display_errors off outside development and log errors to a file.
- Use one wrapper function for every fetch call that checks response.ok before response.json().
- Leave the closing ?> tag off PHP-only files so no trailing newline is output.
- Always create the JSON with json_encode().
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
Back end
Warning: Cannot modify header information - headers already sent
Warning: session_start(): Session cannot be started after headers have already been sent
PHP sent output to the browser before your header(), setcookie() or session_start() call, and headers must go first.
3 causes, each with a fixBack end
json_decode() gives NULL for JSON that looks valid
Syntax error
json_decode returns null when it cannot decode the input, and also when the JSON really is null.
4 causes, each with a fixWeb apps
CodeIgniter 3 shows 'The action you have requested is not allowed.'
403 Forbidden
With csrf_protection on, CodeIgniter 3 compares the POST token with the CSRF cookie and returns this 403 when either is missing or they differ.
4 causes, each with a fixWeb apps
jQuery says it cannot use the in operator on a response from PHP
Uncaught TypeError: Cannot use 'in' operator to search for '324' in
The in operator only works on objects, and jQuery.each() uses it.
3 causes, each with a fixWeb apps
WordPress media upload fails with "Unexpected response from the server"
413 Request Entity Too Large
WordPress shows this message when the upload request did not come back as the JSON it expects.
3 causes, each with a fixWeb apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fix