{"record":{"id":"27880ab72a63d50a","repo":"soxoj/maigret","slug":"problem-parsing-json-contents-from-file-filename","errorCode":null,"errorMessage":"Problem parsing json contents from file '{filename}':  {str(error)}.","messagePattern":"Problem parsing json contents from file '(.+?)':  (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maigret/sites.py","lineNumber":590,"sourceCode":"                data = response.json()\n            except Exception as error:\n                raise ValueError(\n                    f\"Problem parsing json contents at \" f\"'{url}':  {str(error)}.\"\n                )\n        else:\n            raise FileNotFoundError(\n                f\"Bad response while accessing \" f\"data file URL '{url}'.\"\n            )\n\n        return self.load_from_json(data)\n\n    def load_from_file(self, filename: \"str\") -> \"MaigretDatabase\":\n        try:\n            with open(filename, \"r\", encoding=\"utf-8\") as file:\n                try:\n                    data = json.load(file)\n                except Exception as error:\n                    raise ValueError(\n                        f\"Problem parsing json contents from \"\n                        f\"file '{filename}':  {str(error)}.\"\n                    )\n        except FileNotFoundError as error:\n            raise FileNotFoundError(\n                f\"Problem while attempting to access \" f\"data file '{filename}'.\"\n            ) from error\n\n        return self.load_from_json(data)\n\n    def get_scan_stats(self, sites_dict):\n        sites = sites_dict or self.sites_dict\n        found_flags: Dict[str, int] = {}\n        for _, s in sites.items():\n            if \"presense_flag\" in s.stats:\n                flag = s.stats[\"presense_flag\"]\n                found_flags[flag] = found_flags.get(flag, 0) + 1\n","sourceCodeStart":572,"sourceCodeEnd":608,"githubUrl":"https://github.com/soxoj/maigret/blob/41674631a92a4c3ea630d046ba125cb750c03798/maigret/sites.py#L572-L608","documentation":"load_from_file() opens a local file and calls json.load on it; if the file exists and opens but its contents are not valid JSON, a ValueError('Problem parsing json contents from file ...') is raised with the JSONDecodeError details appended. This indicates the path resolved to a readable file whose bytes are not a well-formed JSON document — typically truncated downloads, HTML saved with a .json extension, BOM issues, or empty files.","triggerScenarios":"Calling db.load_from_file('data.json') where data.json is truncated (interrupted download), contains a UTF-8 BOM plus malformed syntax, is empty (0 bytes), or holds YAML/HTML/text instead of JSON. Also triggered by JSON with trailing commas or comments, which Python's json module rejects.","commonSituations":"A partially downloaded maigret resources/data.json (script killed mid-download), a file created by redirecting an HTML error page into .json, hand-edited JSON with trailing commas/comments, or Windows editors saving with a BOM.","solutions":["Validate the file standalone: python -m json.tool data.json — the output shows the exact line/column of the syntax error.","Re-download a known-good copy from the maigret repository (resources/data.json) or restore from version control.","If hand-edited, remove trailing commas and comments; JSON must be strict.","If the file has a UTF-8 BOM, re-save without BOM (encoding='utf-8-sig' read is not used by this loader)."],"exampleFix":"// before\n# data.json was truncated by an interrupted download\ndb = MaigretDatabase().load_from_file('data.json')\n\n// after\n# verify then load\nimport json\njson.load(open('data.json', encoding='utf-8'))  # throws with exact position if bad\ndb = MaigretDatabase().load_from_file('data.json')","handlingStrategy":"validation","validationCode":"import json, os\n\npath = 'data.json'\nassert os.path.isfile(path), f'missing {path}'\njson.load(open(path, encoding='utf-8'))  # fails fast with exact line/col\n\ndb = MaigretDatabase().load_from_file(path)","typeGuard":"import json\n\ndef is_valid_json_file(path: str) -> bool:\n    try:\n        with open(path, encoding='utf-8') as f:\n            json.load(f)\n        return True\n    except (json.JSONDecodeError, OSError):\n        return False","tryCatchPattern":"try:\n    db = MaigretDatabase().load_from_file(path)\nexcept ValueError as err:\n    logger.error('corrupt data file %s: %s — re-downloading', path, err)\n    download_fresh(path)\n    db = MaigretDatabase().load_from_file(path)","preventionTips":["Validate downloads with json.tool before saving/using them.","Write downloads atomically (temp file + os.replace) so partial files never carry the final name.","Keep data files in version control or regenerate from a verified source."],"tags":["json","parsing","file-io","maigret"],"backgroundTag":"json-parse-error","analyzedSha":"41674631a92a4c3ea630d046ba125cb750c03798","analyzedAt":"2026-08-27T02:53:58.946Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}