Bugfix/JSON5 Parsing (#5201)

use json5 for parsing input data
This commit is contained in:
Henry Heng
2025-09-12 17:27:03 +01:00
committed by GitHub
parent e002e617df
commit 4af067a444
12 changed files with 32 additions and 43 deletions
@@ -9,6 +9,7 @@ import {
} from '../../../src/Interface'
import axios, { AxiosRequestConfig } from 'axios'
import { getCredentialData, getCredentialParam, processTemplateVariables } from '../../../src/utils'
import JSON5 from 'json5'
import { DataSource } from 'typeorm'
import { BaseMessageLike } from '@langchain/core/messages'
import { updateFlowState } from '../utils'
@@ -167,9 +168,7 @@ class ExecuteFlow_Agentflow implements INode {
let overrideConfig = nodeData.inputs?.executeFlowOverrideConfig
if (typeof overrideConfig === 'string' && overrideConfig.startsWith('{') && overrideConfig.endsWith('}')) {
try {
// Handle escaped square brackets and other common escape sequences
const unescapedConfig = overrideConfig.replace(/\\(\[|\])/g, '$1')
overrideConfig = JSON.parse(unescapedConfig)
overrideConfig = JSON5.parse(overrideConfig)
} catch (parseError) {
throw new Error(`Invalid JSON in executeFlowOverrideConfig: ${parseError.message}`)
}
@@ -4,6 +4,7 @@ import FormData from 'form-data'
import * as querystring from 'querystring'
import { getCredentialData, getCredentialParam } from '../../../src/utils'
import { secureAxiosRequest } from '../../../src/httpSecurity'
import JSON5 from 'json5'
class HTTP_Agentflow implements INode {
label: string
@@ -19,34 +20,13 @@ class HTTP_Agentflow implements INode {
credential: INodeParams
inputs: INodeParams[]
private sanitizeJsonString(jsonString: string): string {
// Remove common problematic escape sequences that are not valid JSON
let sanitized = jsonString
// Remove escaped square brackets (not valid JSON)
.replace(/\\(\[|\])/g, '$1')
// Fix unquoted string values in JSON (simple case)
.replace(/:\s*([a-zA-Z][a-zA-Z0-9]*)\s*([,}])/g, ': "$1"$2')
// Fix trailing commas
.replace(/,(\s*[}\]])/g, '$1')
return sanitized
}
private parseJsonBody(body: string): any {
try {
// First try to parse as-is
return JSON.parse(body)
return JSON5.parse(body)
} catch (error) {
try {
// If that fails, try to sanitize and parse
const sanitized = this.sanitizeJsonString(body)
return JSON.parse(sanitized)
} catch (sanitizeError) {
// If sanitization also fails, throw the original error with helpful message
throw new Error(
`Invalid JSON format in body. Original error: ${error.message}. Please ensure your JSON is properly formatted with quoted strings and valid escape sequences.`
)
}
throw new Error(
`Invalid JSON format in body. Original error: ${error.message}. Please ensure your JSON is properly formatted with quoted strings and valid escape sequences.`
)
}
}
@@ -1,4 +1,5 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import JSON5 from 'json5'
class Iteration_Agentflow implements INode {
label: string
@@ -41,10 +42,10 @@ class Iteration_Agentflow implements INode {
// Helper function to clean JSON strings with redundant backslashes
const safeParseJson = (str: string): string => {
try {
return JSON.parse(str)
return JSON5.parse(str)
} catch {
// Try parsing after cleaning
return JSON.parse(str.replace(/\\(["'[\]{}])/g, '$1'))
return JSON5.parse(str.replace(/\\(["'[\]{}])/g, '$1'))
}
}