import json


def _clean_output_json(output_json: str) -> str:
    """Make JSON output deterministic and nicer to read."""
    try:
        output = json.loads(output_json)
    except json.JSONDecodeError:
        raise ValueError(
            f"Instead of JSON, output was:\n--- output start ---\n{output_json}\n--- output end ---"
        )

    return json.dumps(output, indent=2, sort_keys=True)