Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

ErrorSyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Short 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.

  • Applies to PHP 8.3, Firefox 130, Chrome 130
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.php
  2. 2Match what you see, then jump to its fix

    1. 1The body starts with <br /> or <b>Warning</b> or <b>Notice</b>A PHP warning or notice is printed before the JSONFix
    2. 2The body starts with <!DOCTYPE or <html, or the status is 404 or 500The request got an HTML page, not your script's answerFix
    3. 3The 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. 4The body starts with { or [ but the message points to a later character, or you built the JSON by handThe text is not valid JSONFix

What does the error look like?

  • SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
  • SyntaxError: 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.log

3Verify

The first byte of the response is now { or [.

curl -s https://example.com/api/items.php | head -c 1

Confirmed by php.net

Did this fix it?

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.php

2Fix

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 1

Confirmed by developer.mozilla.org

Did this fix it?

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 -A

2Fix

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 12

Confirmed by php.net

Did this fix it?

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.tool

2Fix

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.tool

Confirmed by developer.mozilla.org

Did this fix it?

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

Web 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.