{"record":{"id":"70c23073f3d15e42","repo":"FlowiseAI/Flowise","slug":"invalid-json-format-in-body-original-error-err","errorCode":null,"errorMessage":"Invalid JSON format in body. Original error: ${error.message}. After cleanup attempts: ${secondError.message}. 3rd attempt: ${thirdError.message}. Final attempt: ${fourthError.message}.\\n\\nCommon fixes:\\n${suggestions.join('\\n')}\\n\\nReceived body: ${body.substring(0, 200)}${body.length > 200 ? '...' : ''}","messagePattern":"Invalid JSON format in body\\. Original error: (.+?)\\. After cleanup attempts: (.+?)\\. 3rd attempt: (.+?)\\. Final attempt: (.+?)\\.\\\\n\\\\nCommon fixes:\\\\n(.+?)\\\\n\\\\nReceived body: (.+?)(.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/components/src/utils.ts","lineNumber":1947,"sourceCode":"                    const finalCleanedBody = body\n                        // eslint-disable-next-line\n                        .replace(/\\\\(?=[\\[\\]{}])/g, '') // Basic escape cleanup\n                        .replace(/,(\\s*[}\\]])/g, '$1') // Remove trailing commas\n                        .trim()\n\n                    return JSON.parse(finalCleanedBody)\n                } catch (fourthError) {\n                    // Provide comprehensive error message with suggestions\n                    const suggestions = [\n                        '• Ensure all strings are enclosed in double quotes',\n                        '• Remove trailing commas',\n                        '• Remove comments (// or /* */)',\n                        '• Escape special characters properly (\\\\n for newlines, \\\\\" for quotes)',\n                        '• Use double quotes instead of single quotes',\n                        '• Remove unnecessary backslashes before brackets [ ] { }'\n                    ]\n\n                    throw new Error(\n                        `Invalid JSON format in body. Original error: ${error.message}. ` +\n                            `After cleanup attempts: ${secondError.message}. 3rd attempt: ${thirdError.message}. Final attempt: ${fourthError.message}.\\n\\n` +\n                            `Common fixes:\\n${suggestions.join('\\n')}\\n\\n` +\n                            `Received body: ${body.substring(0, 200)}${body.length > 200 ? '...' : ''}`\n                    )\n                }\n            }\n        }\n    }\n}\n\n/**\n * Parse a value against a Zod schema with automatic type conversion for common type mismatches\n * @param schema - The Zod schema to parse against\n * @param arg - The value to parse\n * @param maxDepth - Maximum recursion depth to prevent infinite loops (default: 10)\n * @returns The parsed value\n * @throws Error if parsing fails after attempting type conversions","sourceCodeStart":1929,"sourceCodeEnd":1965,"githubUrl":"https://github.com/FlowiseAI/Flowise/blob/abe4a8601a058047b350c260676826e21dd14101/packages/components/src/utils.ts#L1929-L1965","documentation":"Thrown after JSON/JSON5 parsing fails four successive attempts (JSON5 original, JSON5 cleaned, JSON.parse original, JSON.parse cleaned). The message aggregates all four error messages plus remediation hints and a 200-char preview of the received body. It indicates the input is structurally not parseable JSON even after common cleanup (trailing commas, single quotes, comments, backslash escapes).","triggerScenarios":"Body is HTML (e.g. an error page from a proxy) instead of JSON; body is truncated by a proxy/gateway mid-payload; body uses a non-JSON format (YAML, XML, form-encoded); nested string contains an unescaped control character; surrogate pairs broken by truncation.","commonSituations":"Reverse proxy returning its own HTML error page with Content-Type application/json; client sending multipart/form-data but the handler expects JSON; load balancer truncating oversized bodies; AI-generated payloads with subtle syntax errors that the cleanup heuristics can't fix.","solutions":["Inspect the 200-char preview in the message to see what was actually received (HTML? truncated? wrong format?).","Fix the upstream sender to send well-formed JSON with double quotes, no trailing commas, no comments.","If a proxy injects error pages, ensure requests reach the API route (check Content-Type and routing).","Increase any body-size limits so payloads aren't truncated before parsing."],"exampleFix":"// before — single throw with aggregated message\nthrow new Error(`Invalid JSON format in body. Original error: ${error.message}. ... Received body: ${body.substring(0,200)}...`)\n\n// caller-side: guard before calling the parser\nif (!body || !body.trim().startsWith('{') && !body.trim().startsWith('[')) {\n  return res.status(400).json({ error: 'Expected JSON object or array', preview: body.substring(0, 200) })\n}","handlingStrategy":"validation","validationCode":"function looksLikeJson(body: string): boolean {\n  const t = (body || '').trim()\n  return t.startsWith('{') || t.startsWith('[')\n}","typeGuard":"function isJsonArrayOrObject(body: string): boolean {\n  const t = body.trim()\n  if (!t.startsWith('{') && !t.startsWith('[')) return false\n  try { JSON.parse(t); return true } catch { return false }\n}","tryCatchPattern":"if (!looksLikeJson(body)) {\n  throw new Error(`Expected JSON object or array. Received (${body.length} chars): ${body.substring(0, 200)}`)\n}","preventionTips":["Reject non-JSON bodies at the route boundary (400) before parsing.","Ensure reverse proxies return JSON error bodies, not HTML, on application/json routes.","Cap request body size to avoid truncated-payload parsing failures."],"tags":["json","json5","parsing","request-body","validation"],"backgroundTag":null,"analyzedSha":"abe4a8601a058047b350c260676826e21dd14101","analyzedAt":"2026-08-12T16:04:40.823Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}