soxoj/maigret · error · ValueError

Problem parsing json contents from file '{filename}': {str(

Error message

Problem parsing json contents from file '{filename}':  {str(error)}.

What it means

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.

Source

Thrown at maigret/sites.py:590

                data = response.json()
            except Exception as error:
                raise ValueError(
                    f"Problem parsing json contents at " f"'{url}':  {str(error)}."
                )
        else:
            raise FileNotFoundError(
                f"Bad response while accessing " f"data file URL '{url}'."
            )

        return self.load_from_json(data)

    def load_from_file(self, filename: "str") -> "MaigretDatabase":
        try:
            with open(filename, "r", encoding="utf-8") as file:
                try:
                    data = json.load(file)
                except Exception as error:
                    raise ValueError(
                        f"Problem parsing json contents from "
                        f"file '{filename}':  {str(error)}."
                    )
        except FileNotFoundError as error:
            raise FileNotFoundError(
                f"Problem while attempting to access " f"data file '{filename}'."
            ) from error

        return self.load_from_json(data)

    def get_scan_stats(self, sites_dict):
        sites = sites_dict or self.sites_dict
        found_flags: Dict[str, int] = {}
        for _, s in sites.items():
            if "presense_flag" in s.stats:
                flag = s.stats["presense_flag"]
                found_flags[flag] = found_flags.get(flag, 0) + 1

View on GitHub (pinned to 41674631a9)

Solutions

  1. Validate the file standalone: python -m json.tool data.json — the output shows the exact line/column of the syntax error.
  2. Re-download a known-good copy from the maigret repository (resources/data.json) or restore from version control.
  3. If hand-edited, remove trailing commas and comments; JSON must be strict.
  4. If the file has a UTF-8 BOM, re-save without BOM (encoding='utf-8-sig' read is not used by this loader).

Example fix

// before
# data.json was truncated by an interrupted download
db = MaigretDatabase().load_from_file('data.json')

// after
# verify then load
import json
json.load(open('data.json', encoding='utf-8'))  # throws with exact position if bad
db = MaigretDatabase().load_from_file('data.json')
Defensive patterns

Strategy: validation

Validate before calling

import json, os

path = 'data.json'
assert os.path.isfile(path), f'missing {path}'
json.load(open(path, encoding='utf-8'))  # fails fast with exact line/col

db = MaigretDatabase().load_from_file(path)

Type guard

import json

def is_valid_json_file(path: str) -> bool:
    try:
        with open(path, encoding='utf-8') as f:
            json.load(f)
        return True
    except (json.JSONDecodeError, OSError):
        return False

Try / catch

try:
    db = MaigretDatabase().load_from_file(path)
except ValueError as err:
    logger.error('corrupt data file %s: %s — re-downloading', path, err)
    download_fresh(path)
    db = MaigretDatabase().load_from_file(path)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of soxoj/maigret@41674631a9 (2026-08-27). Data as JSON: /api/errors/27880ab72a63d50a. Report an issue: GitHub.