symfony/translation · error · InvalidResourceException

Error parsing JSON:

Error message

Error parsing JSON: 

What it means

JsonFileLoader parses translation files with json_decode; if json_last_error() is non-zero the raw JSON is invalid and the loader throws InvalidResourceException including a human-readable JSON error description. The message ends with the parsed JSON error text (e.g. syntax error, control character).

Solutions

  1. Run the file through a JSON validator or `php -r 'json_decode(file_get_contents("file.json")); echo json_last_error_msg();'` to see the exact error and position.
  2. Fix the syntax error at the reported location (trailing comma, unquoted key, etc.).
  3. Re-encode the file as plain UTF-8 without BOM.
  4. Regenerate the file if it was truncated by a failed export/deploy.

Example fix

// before (messages.json)
{"key": "value",}  // trailing comma
// after
{"key": "value"}
Defensive patterns

Strategy: validation

Validate before calling

json_decode(file_get_contents($file), true);
if (json_last_error() !== JSON_ERROR_NONE) {
    throw new \RuntimeException(sprintf('%s is invalid JSON: %s', $file, json_last_error_msg()));
}

Try / catch

try {
    $catalogue = $loader->load($file, $locale);
} catch (InvalidResourceException $e) {
    error_log('JSON translation file invalid: '.$e->getMessage());
    $catalogue = new MessageCatalogue($locale);
}

Prevention

When it happens

Trigger: loadResource() on a .json translation file containing malformed JSON — trailing commas, BOM, comments, single quotes, truncated output from an editor or build step.

Common situations: Hand-edited translation files with syntax errors, concatenation bugs in build scripts, files saved with UTF-8 BOM, non-UTF8 encoding, empty or half-written files.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of symfony/translation@ae9e8a51bc (2026-09-15). Data as JSON: /api/errors/e4d5afcdec2b42a5. Report an issue: GitHub.

Appendix: source

Thrown at Loader/JsonFileLoader.php:30

namespace Symfony\Component\Translation\Loader;

use Symfony\Component\Translation\Exception\InvalidResourceException;

/**
 * JsonFileLoader loads translations from an json file.
 *
 * @author singles
 */
class JsonFileLoader extends FileLoader
{
    protected function loadResource(string $resource): array
    {
        $messages = [];
        if ($data = file_get_contents($resource)) {
            $messages = json_decode($data, true);

            if (0 < $errorCode = json_last_error()) {
                throw new InvalidResourceException('Error parsing JSON: '.$this->getJSONErrorMessage($errorCode));
            }
        }

        return $messages;
    }

    /**
     * Translates JSON_ERROR_* constant into meaningful message.
     */
    private function getJSONErrorMessage(int $errorCode): string
    {
        return match ($errorCode) {
            \JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
            \JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch',
            \JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
            \JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
            \JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
            default => 'Unknown error',

View on GitHub (pinned to ae9e8a51bc)