composer/composer · error · ParsingException

"{file}" does not contain valid JSON {result_message}

Error message

"{file}" does not contain valid JSON
{result_message}

What it means

Thrown by JsonFile::validateSyntax() when parsing a JSON file that contains syntax errors. The code at JsonFile.php:378-402 runs the Seld/JsonLint parser; if lint() returns a non-null result (parse error), and a $file path is provided, it throws a ParsingException naming the file and including the linter's message with line/column details. This is the file-based variant of error 18.

Source

Thrown at src/Composer/Json/JsonFile.php:398

        if (null === $result) {
            if (defined('JSON_ERROR_UTF8') && JSON_ERROR_UTF8 === json_last_error()) {
                if ($file === null) {
                    throw new \UnexpectedValueException('The input is not UTF-8, could not parse as JSON');
                } else {
                    throw new \UnexpectedValueException('"' . $file . '" is not UTF-8, could not parse as JSON');
                }
            }

            return true;
        }

        if ($file === null) {
            throw new ParsingException(
                'The input does not contain valid JSON' . "\n" . $result->getMessage(),
                $result->getDetails()
            );
        } else {
            throw new ParsingException(
                '"' . $file . '" does not contain valid JSON' . "\n" . $result->getMessage(),
                $result->getDetails()
            );
        }
    }

    public static function detectIndenting(?string $json): string
    {
        if (Preg::isMatchStrictGroups('#^([ \t]+)"#m', $json ?? '', $match)) {
            return $match[1];
        }

        return self::INDENT_DEFAULT;
    }
}

View on GitHub (pinned to c435d285c9)

Solutions

  1. Read the ParsingException message; the linter shows the exact line, column, and a caret pointing to the error.
  2. Run 'composer validate' which reports JSON syntax errors with location.
  3. Use a JSON-aware editor or 'python -m json.tool composer.json' to find the syntax error.
  4. Check for merge-conflict markers, BOM characters, or smart quotes if the error location seems wrong.
  5. Restore from version control and re-apply changes carefully: 'git checkout -- composer.json'.

Example fix

// before: composer.json (trailing comma)
{
    "name": "test/package",
    "require": {
        "monolog/monolog": "^2.0",
    }
}
// ParsingException: "composer.json" does not contain valid JSON

// after
{
    "name": "test/package",
    "require": {
        "monolog/monolog": "^2.0"
    }
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate the JSON file before reading
use Composer\Json\JsonFile;
$file = new JsonFile('composer.json');
try {
    $file->validateSyntax(file_get_contents('composer.json'), 'composer.json');
} catch (\Seld\JsonLint\ParsingException $e) {
    fwrite(STDERR, $e->getMessage());
    exit(1);
}
$data = $file->read();

Type guard

function jsonFileIsValidSyntax(string $path): bool {
    if (!file_exists($path)) {
        return false;
    }
    try {
        \Composer\Json\JsonFile::parseJson(file_get_contents($path), $path);
        return true;
    } catch (\Throwable $e) {
        return false;
    }
}

Try / catch

try {
    $data = $file->read();
} catch (\Seld\JsonLint\ParsingException $e) {
    // The message includes line, column, and a caret pointer to the error
    fwrite(STDERR, $e->getMessage());
    exit(1);
}

Prevention

When it happens

Trigger: Calling $jsonFile->read() or JsonFile::parseJson($json, $path) on a composer.json, composer.lock, or any JSON config file with syntax errors. The linter provides precise error location including a visual caret under the problematic token.

Common situations: Manual edits to composer.json introducing a trailing comma, unquoted key, or missing brace. Merge conflicts leaving conflict markers in the JSON. Encoding issues (non-UTF-8 BOM). Copy-paste from formatted text introducing smart quotes.

Related errors


AI-assisted analysis of composer/composer@c435d285c9 (2026-08-07). Data as JSON: /api/errors/c03509aff7eebd8d. Report an issue: GitHub.