Fixes problematic json parsing in Iteration.ts (#5077)

Resolves the json parsing issue in Iteration.ts gracefully
This commit is contained in:
Purshotam Bohra 2025-08-15 16:34:11 +05:30 committed by GitHub
parent 55f8f69060
commit 44087bc706
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 3 deletions

View File

@ -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')