{"record":{"id":"e4d5afcdec2b42a5","repo":"symfony/translation","slug":"error-parsing-json","errorCode":null,"errorMessage":"Error parsing JSON: ","messagePattern":"Error parsing JSON: ","errorType":"exception","errorClass":"InvalidResourceException","httpStatus":null,"severity":"error","filePath":"Loader/JsonFileLoader.php","lineNumber":30,"sourceCode":"namespace Symfony\\Component\\Translation\\Loader;\n\nuse Symfony\\Component\\Translation\\Exception\\InvalidResourceException;\n\n/**\n * JsonFileLoader loads translations from an json file.\n *\n * @author singles\n */\nclass JsonFileLoader extends FileLoader\n{\n    protected function loadResource(string $resource): array\n    {\n        $messages = [];\n        if ($data = file_get_contents($resource)) {\n            $messages = json_decode($data, true);\n\n            if (0 < $errorCode = json_last_error()) {\n                throw new InvalidResourceException('Error parsing JSON: '.$this->getJSONErrorMessage($errorCode));\n            }\n        }\n\n        return $messages;\n    }\n\n    /**\n     * Translates JSON_ERROR_* constant into meaningful message.\n     */\n    private function getJSONErrorMessage(int $errorCode): string\n    {\n        return match ($errorCode) {\n            \\JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',\n            \\JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch',\n            \\JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',\n            \\JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',\n            \\JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',\n            default => 'Unknown error',","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/symfony/translation/blob/ae9e8a51bc29780d63c7f9efd6005d7c875e100b/Loader/JsonFileLoader.php#L12-L48","documentation":"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).","triggerScenarios":"loadResource() on a .json translation file containing malformed JSON — trailing commas, BOM, comments, single quotes, truncated output from an editor or build step.","commonSituations":"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.","solutions":["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.","Fix the syntax error at the reported location (trailing comma, unquoted key, etc.).","Re-encode the file as plain UTF-8 without BOM.","Regenerate the file if it was truncated by a failed export/deploy."],"exampleFix":"// before (messages.json)\n{\"key\": \"value\",}  // trailing comma\n// after\n{\"key\": \"value\"}","handlingStrategy":"validation","validationCode":"json_decode(file_get_contents($file), true);\nif (json_last_error() !== JSON_ERROR_NONE) {\n    throw new \\RuntimeException(sprintf('%s is invalid JSON: %s', $file, json_last_error_msg()));\n}","typeGuard":null,"tryCatchPattern":"try {\n    $catalogue = $loader->load($file, $locale);\n} catch (InvalidResourceException $e) {\n    error_log('JSON translation file invalid: '.$e->getMessage());\n    $catalogue = new MessageCatalogue($locale);\n}","preventionTips":["Lint all translation JSON files in CI (e.g. with json_decode or a linter)","Save files as UTF-8 without BOM","Avoid hand-editing generated catalogues; regenerate from source","Never allow trailing commas or comments in JSON"],"tags":["json","translation","parse-error"],"backgroundTag":"json-parse-error","analyzedSha":"ae9e8a51bc29780d63c7f9efd6005d7c875e100b","analyzedAt":"2026-09-15T22:29:15.305Z","contentChangedAt":"2026-09-15T22:29:15.305Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}