{"record":{"id":"17001710b601bee3","repo":"Zie619/n8n-workflows","slug":"invalid-json-in-workflow-file-str-e","errorCode":null,"errorMessage":"Invalid JSON in workflow file: {str(e)}","messagePattern":"Invalid JSON in workflow file: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"api_server.py","lineNumber":492,"sourceCode":"                status_code=404,\n                detail=f\"Workflow file '{filename}' not found on filesystem\",\n            )\n\n        with open(matching_file, \"r\", encoding=\"utf-8\") as f:\n            data = json.load(f)\n\n        nodes = data.get(\"nodes\", [])\n        connections = data.get(\"connections\", {})\n\n        # Generate Mermaid diagram\n        diagram = generate_mermaid_diagram(nodes, connections)\n\n        return {\"diagram\": diagram}\n    except HTTPException:\n        raise\n    except json.JSONDecodeError as e:\n        print(f\"Error parsing JSON in {filename}: {str(e)}\")\n        raise HTTPException(\n            status_code=400, detail=f\"Invalid JSON in workflow file: {str(e)}\"\n        )\n    except Exception as e:\n        print(f\"Error generating diagram for {filename}: {str(e)}\")\n        raise HTTPException(\n            status_code=500, detail=f\"Error generating diagram: {str(e)}\"\n        )\n\n\ndef generate_mermaid_diagram(nodes: List[Dict], connections: Dict) -> str:\n    \"\"\"Generate Mermaid.js flowchart code from workflow nodes and connections.\"\"\"\n    if not nodes:\n        return \"graph TD\\n  EmptyWorkflow[No nodes found in workflow]\"\n\n    # Create mapping for node names to ensure valid mermaid IDs\n    mermaid_ids = {}\n    for i, node in enumerate(nodes):\n        node_id = f\"node{i}\"","sourceCodeStart":474,"sourceCodeEnd":510,"githubUrl":"https://github.com/Zie619/n8n-workflows/blob/94007c1445d9258a7da116646b79473e7c7c3282/api_server.py#L474-L510","documentation":"A 400 from GET /api/workflows/{filename}/diagram raised by the dedicated except json.JSONDecodeError branch: the workflow file was found and opened, but json.load() failed to parse it. The parser's message (line/column, 'Expecting value', 'Extra data') is embedded in the detail, and the server prints 'Error parsing JSON in {filename}'. This branch fires before the generic 500 handler, distinguishing malformed content from other failures.","triggerScenarios":"Downloading a diagram for a file truncated mid-write, containing a UTF-8 BOM, holding concatenated JSON objects ('Extra data'), or actually being HTML/error text saved with a .json extension.","commonSituations":"Hand-edited workflow files with trailing commas or unescaped quotes; files synced incompletely; Windows editors saving with BOM; empty files from a failed export.","solutions":["Validate the file locally: python -m json.tool workflows/<subdir>/<file>.json — it reports the same error with position.","Fix the JSON at the reported line/column (the detail string includes it) or restore the file from git.","Re-save files as UTF-8 without BOM and ensure writes are atomic (write temp, rename).","After repair, the diagram endpoint will parse it without reindexing (diagrams read the file directly)."],"exampleFix":"# before (trailing comma breaks parsing)\n{ \"name\": \"flow\", \"nodes\": [], }\n\n# after\n{ \"name\": \"flow\", \"nodes\": [] }","handlingStrategy":"validation","validationCode":"import json, pathlib\n\ndef parses_cleanly(path):\n    try:\n        json.loads(pathlib.Path(path).read_text(encoding='utf-8-sig'))\n        return True\n    except json.JSONDecodeError:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    d = client.get(f'/api/workflows/{name}/diagram').json()['diagram']\nexcept HTTPError as e:\n    detail = e.response.json().get('detail', '')\n    if e.response.status_code == 400 and 'Invalid JSON' in detail:\n        quarantine(name, detail)  # file content bug — fix the JSON","preventionTips":["Validate every workflow JSON in CI with json.load before commit.","Save UTF-8 without BOM; write files atomically.","Use the detail's line/column to fix the file, then the diagram recovers without reindex."],"tags":["fastapi","json","validation","http-400","diagram"],"backgroundTag":null,"analyzedSha":"94007c1445d9258a7da116646b79473e7c7c3282","analyzedAt":"2026-08-15T04:10:37.591Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}