From 44087bc706dfbe255c10c09546d6db0e1a72dcdd Mon Sep 17 00:00:00 2001 From: Purshotam Bohra <67221507+PBJI@users.noreply.github.com> Date: Fri, 15 Aug 2025 16:34:11 +0530 Subject: [PATCH] Fixes problematic json parsing in Iteration.ts (#5077) Resolves the json parsing issue in Iteration.ts gracefully --- .../components/nodes/agentflow/Iteration/Iteration.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/components/nodes/agentflow/Iteration/Iteration.ts b/packages/components/nodes/agentflow/Iteration/Iteration.ts index 048035fb2..53026bb53 100644 --- a/packages/components/nodes/agentflow/Iteration/Iteration.ts +++ b/packages/components/nodes/agentflow/Iteration/Iteration.ts @@ -39,12 +39,17 @@ class Iteration_Agentflow implements INode { const iterationInput = nodeData.inputs?.iterationInput // Helper function to clean JSON strings with redundant backslashes - const cleanJsonString = (str: string): string => { - return str.replace(/\\(["'[\]{}])/g, '$1') + const safeParseJson = (str: string): string => { + try { + return JSON.parse(str) + } catch { + // Try parsing after cleaning + return JSON.parse(str.replace(/\\(["'[\]{}])/g, '$1')) + } } const iterationInputArray = - typeof iterationInput === 'string' && iterationInput !== '' ? JSON.parse(cleanJsonString(iterationInput)) : iterationInput + typeof iterationInput === 'string' && iterationInput !== '' ? safeParseJson(iterationInput) : iterationInput if (!iterationInputArray || !Array.isArray(iterationInputArray)) { throw new Error('Invalid input array')