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' } from '../../../src/Interface'
import axios, { AxiosRequestConfig } from 'axios' import axios, { AxiosRequestConfig } from 'axios'
import { getCredentialData, getCredentialParam, processTemplateVariables } from '../../../src/utils' import { getCredentialData, getCredentialParam, processTemplateVariables } from '../../../src/utils'
import JSON5 from 'json5'
import { DataSource } from 'typeorm' import { DataSource } from 'typeorm'
import { BaseMessageLike } from '@langchain/core/messages' import { BaseMessageLike } from '@langchain/core/messages'
import { updateFlowState } from '../utils' import { updateFlowState } from '../utils'
@@ -167,9 +168,7 @@ class ExecuteFlow_Agentflow implements INode {
let overrideConfig = nodeData.inputs?.executeFlowOverrideConfig let overrideConfig = nodeData.inputs?.executeFlowOverrideConfig
if (typeof overrideConfig === 'string' && overrideConfig.startsWith('{') && overrideConfig.endsWith('}')) { if (typeof overrideConfig === 'string' && overrideConfig.startsWith('{') && overrideConfig.endsWith('}')) {
try { try {
// Handle escaped square brackets and other common escape sequences overrideConfig = JSON5.parse(overrideConfig)
const unescapedConfig = overrideConfig.replace(/\\(\[|\])/g, '$1')
overrideConfig = JSON.parse(unescapedConfig)
} catch (parseError) { } catch (parseError) {
throw new Error(`Invalid JSON in executeFlowOverrideConfig: ${parseError.message}`) throw new Error(`Invalid JSON in executeFlowOverrideConfig: ${parseError.message}`)
} }
@@ -4,6 +4,7 @@ import FormData from 'form-data'
import * as querystring from 'querystring' import * as querystring from 'querystring'
import { getCredentialData, getCredentialParam } from '../../../src/utils' import { getCredentialData, getCredentialParam } from '../../../src/utils'
import { secureAxiosRequest } from '../../../src/httpSecurity' import { secureAxiosRequest } from '../../../src/httpSecurity'
import JSON5 from 'json5'
class HTTP_Agentflow implements INode { class HTTP_Agentflow implements INode {
label: string label: string
@@ -19,34 +20,13 @@ class HTTP_Agentflow implements INode {
credential: INodeParams credential: INodeParams
inputs: 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 { private parseJsonBody(body: string): any {
try { try {
// First try to parse as-is return JSON5.parse(body)
return JSON.parse(body)
} catch (error) { } catch (error) {
try { throw new Error(
// If that fails, try to sanitize and parse `Invalid JSON format in body. Original error: ${error.message}. Please ensure your JSON is properly formatted with quoted strings and valid escape sequences.`
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.`
)
}
} }
} }
@@ -1,4 +1,5 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import JSON5 from 'json5'
class Iteration_Agentflow implements INode { class Iteration_Agentflow implements INode {
label: string label: string
@@ -41,10 +42,10 @@ class Iteration_Agentflow implements INode {
// Helper function to clean JSON strings with redundant backslashes // Helper function to clean JSON strings with redundant backslashes
const safeParseJson = (str: string): string => { const safeParseJson = (str: string): string => {
try { try {
return JSON.parse(str) return JSON5.parse(str)
} catch { } catch {
// Try parsing after cleaning // Try parsing after cleaning
return JSON.parse(str.replace(/\\(["'[\]{}])/g, '$1')) return JSON5.parse(str.replace(/\\(["'[\]{}])/g, '$1'))
} }
} }
@@ -4,6 +4,7 @@ import { MCPToolkit } from '../core'
import { getVars, prepareSandboxVars } from '../../../../src/utils' import { getVars, prepareSandboxVars } from '../../../../src/utils'
import { DataSource } from 'typeorm' import { DataSource } from 'typeorm'
import hash from 'object-hash' import hash from 'object-hash'
import JSON5 from 'json5'
const mcpServerConfig = `{ const mcpServerConfig = `{
"command": "npx", "command": "npx",
@@ -261,7 +262,7 @@ function substituteVariablesInString(str: string, sandbox: any): string {
function convertToValidJSONString(inputString: string) { function convertToValidJSONString(inputString: string) {
try { try {
const jsObject = Function('return ' + inputString)() const jsObject = JSON5.parse(inputString)
return JSON.stringify(jsObject, null, 2) return JSON.stringify(jsObject, null, 2)
} catch (error) { } catch (error) {
console.error('Error converting to JSON:', error) console.error('Error converting to JSON:', error)
@@ -1,6 +1,7 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface' import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, stripHTMLFromToolInput } from '../../../src/utils' import { getBaseClasses, stripHTMLFromToolInput } from '../../../src/utils'
import { desc, RequestParameters, RequestsDeleteTool } from './core' import { desc, RequestParameters, RequestsDeleteTool } from './core'
import JSON5 from 'json5'
const codeExample = `{ const codeExample = `{
"id": { "id": {
@@ -130,7 +131,7 @@ class RequestsDelete_Tools implements INode {
if (queryParamsSchema) obj.queryParamsSchema = queryParamsSchema if (queryParamsSchema) obj.queryParamsSchema = queryParamsSchema
if (maxOutputLength) obj.maxOutputLength = parseInt(maxOutputLength, 10) if (maxOutputLength) obj.maxOutputLength = parseInt(maxOutputLength, 10)
if (headers) { if (headers) {
const parsedHeaders = typeof headers === 'object' ? headers : JSON.parse(stripHTMLFromToolInput(headers)) const parsedHeaders = typeof headers === 'object' ? headers : JSON5.parse(stripHTMLFromToolInput(headers))
obj.headers = parsedHeaders obj.headers = parsedHeaders
} }
@@ -1,6 +1,7 @@
import { z } from 'zod' import { z } from 'zod'
import { DynamicStructuredTool } from '../OpenAPIToolkit/core' import { DynamicStructuredTool } from '../OpenAPIToolkit/core'
import { secureFetch } from '../../../src/httpSecurity' import { secureFetch } from '../../../src/httpSecurity'
import JSON5 from 'json5'
export const desc = `Use this when you need to execute a DELETE request to remove data from a website.` export const desc = `Use this when you need to execute a DELETE request to remove data from a website.`
@@ -22,7 +23,7 @@ const createRequestsDeleteSchema = (queryParamsSchema?: string) => {
// If queryParamsSchema is provided, parse it and add dynamic query params // If queryParamsSchema is provided, parse it and add dynamic query params
if (queryParamsSchema) { if (queryParamsSchema) {
try { try {
const parsedSchema = JSON.parse(queryParamsSchema) const parsedSchema = JSON5.parse(queryParamsSchema)
const queryParamsObject: Record<string, z.ZodTypeAny> = {} const queryParamsObject: Record<string, z.ZodTypeAny> = {}
Object.entries(parsedSchema).forEach(([key, config]: [string, any]) => { Object.entries(parsedSchema).forEach(([key, config]: [string, any]) => {
@@ -108,7 +109,7 @@ export class RequestsDeleteTool extends DynamicStructuredTool {
if (this.queryParamsSchema && params.queryParams && Object.keys(params.queryParams).length > 0) { if (this.queryParamsSchema && params.queryParams && Object.keys(params.queryParams).length > 0) {
try { try {
const parsedSchema = JSON.parse(this.queryParamsSchema) const parsedSchema = JSON5.parse(this.queryParamsSchema)
const pathParams: Array<{ key: string; value: string }> = [] const pathParams: Array<{ key: string; value: string }> = []
Object.entries(params.queryParams).forEach(([key, value]) => { Object.entries(params.queryParams).forEach(([key, value]) => {
@@ -1,6 +1,7 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface' import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, stripHTMLFromToolInput } from '../../../src/utils' import { getBaseClasses, stripHTMLFromToolInput } from '../../../src/utils'
import { desc, RequestParameters, RequestsGetTool } from './core' import { desc, RequestParameters, RequestsGetTool } from './core'
import JSON5 from 'json5'
const codeExample = `{ const codeExample = `{
"id": { "id": {
@@ -130,7 +131,7 @@ class RequestsGet_Tools implements INode {
if (queryParamsSchema) obj.queryParamsSchema = queryParamsSchema if (queryParamsSchema) obj.queryParamsSchema = queryParamsSchema
if (maxOutputLength) obj.maxOutputLength = parseInt(maxOutputLength, 10) if (maxOutputLength) obj.maxOutputLength = parseInt(maxOutputLength, 10)
if (headers) { if (headers) {
const parsedHeaders = typeof headers === 'object' ? headers : JSON.parse(stripHTMLFromToolInput(headers)) const parsedHeaders = typeof headers === 'object' ? headers : JSON5.parse(stripHTMLFromToolInput(headers))
obj.headers = parsedHeaders obj.headers = parsedHeaders
} }
@@ -1,6 +1,7 @@
import { z } from 'zod' import { z } from 'zod'
import { DynamicStructuredTool } from '../OpenAPIToolkit/core' import { DynamicStructuredTool } from '../OpenAPIToolkit/core'
import { secureFetch } from '../../../src/httpSecurity' import { secureFetch } from '../../../src/httpSecurity'
import JSON5 from 'json5'
export const desc = `Use this when you need to execute a GET request to get data from a website.` export const desc = `Use this when you need to execute a GET request to get data from a website.`
@@ -22,7 +23,7 @@ const createRequestsGetSchema = (queryParamsSchema?: string) => {
// If queryParamsSchema is provided, parse it and add dynamic query params // If queryParamsSchema is provided, parse it and add dynamic query params
if (queryParamsSchema) { if (queryParamsSchema) {
try { try {
const parsedSchema = JSON.parse(queryParamsSchema) const parsedSchema = JSON5.parse(queryParamsSchema)
const queryParamsObject: Record<string, z.ZodTypeAny> = {} const queryParamsObject: Record<string, z.ZodTypeAny> = {}
Object.entries(parsedSchema).forEach(([key, config]: [string, any]) => { Object.entries(parsedSchema).forEach(([key, config]: [string, any]) => {
@@ -108,7 +109,7 @@ export class RequestsGetTool extends DynamicStructuredTool {
if (this.queryParamsSchema && params.queryParams && Object.keys(params.queryParams).length > 0) { if (this.queryParamsSchema && params.queryParams && Object.keys(params.queryParams).length > 0) {
try { try {
const parsedSchema = JSON.parse(this.queryParamsSchema) const parsedSchema = JSON5.parse(this.queryParamsSchema)
const pathParams: Array<{ key: string; value: string }> = [] const pathParams: Array<{ key: string; value: string }> = []
Object.entries(params.queryParams).forEach(([key, value]) => { Object.entries(params.queryParams).forEach(([key, value]) => {
@@ -1,6 +1,7 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface' import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, stripHTMLFromToolInput } from '../../../src/utils' import { getBaseClasses, stripHTMLFromToolInput } from '../../../src/utils'
import { RequestParameters, desc, RequestsPostTool } from './core' import { RequestParameters, desc, RequestsPostTool } from './core'
import JSON5 from 'json5'
const codeExample = `{ const codeExample = `{
"name": { "name": {
@@ -140,11 +141,11 @@ class RequestsPost_Tools implements INode {
if (bodySchema) obj.bodySchema = stripHTMLFromToolInput(bodySchema) if (bodySchema) obj.bodySchema = stripHTMLFromToolInput(bodySchema)
if (maxOutputLength) obj.maxOutputLength = parseInt(maxOutputLength, 10) if (maxOutputLength) obj.maxOutputLength = parseInt(maxOutputLength, 10)
if (headers) { if (headers) {
const parsedHeaders = typeof headers === 'object' ? headers : JSON.parse(stripHTMLFromToolInput(headers)) const parsedHeaders = typeof headers === 'object' ? headers : JSON5.parse(stripHTMLFromToolInput(headers))
obj.headers = parsedHeaders obj.headers = parsedHeaders
} }
if (body) { if (body) {
const parsedBody = typeof body === 'object' ? body : JSON.parse(body) const parsedBody = typeof body === 'object' ? body : JSON5.parse(body)
obj.body = parsedBody obj.body = parsedBody
} }
@@ -1,6 +1,7 @@
import { z } from 'zod' import { z } from 'zod'
import { DynamicStructuredTool } from '../OpenAPIToolkit/core' import { DynamicStructuredTool } from '../OpenAPIToolkit/core'
import { secureFetch } from '../../../src/httpSecurity' import { secureFetch } from '../../../src/httpSecurity'
import JSON5 from 'json5'
export const desc = `Use this when you want to execute a POST request to create or update a resource.` export const desc = `Use this when you want to execute a POST request to create or update a resource.`
@@ -27,7 +28,7 @@ const createRequestsPostSchema = (bodySchema?: string) => {
// If bodySchema is provided, parse it and add dynamic body params // If bodySchema is provided, parse it and add dynamic body params
if (bodySchema) { if (bodySchema) {
try { try {
const parsedSchema = JSON.parse(bodySchema) const parsedSchema = JSON5.parse(bodySchema)
const bodyParamsObject: Record<string, z.ZodTypeAny> = {} const bodyParamsObject: Record<string, z.ZodTypeAny> = {}
Object.entries(parsedSchema).forEach(([key, config]: [string, any]) => { Object.entries(parsedSchema).forEach(([key, config]: [string, any]) => {
@@ -1,6 +1,7 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface' import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, stripHTMLFromToolInput } from '../../../src/utils' import { getBaseClasses, stripHTMLFromToolInput } from '../../../src/utils'
import { RequestParameters, desc, RequestsPutTool } from './core' import { RequestParameters, desc, RequestsPutTool } from './core'
import JSON5 from 'json5'
const codeExample = `{ const codeExample = `{
"name": { "name": {
@@ -140,11 +141,11 @@ class RequestsPut_Tools implements INode {
if (bodySchema) obj.bodySchema = stripHTMLFromToolInput(bodySchema) if (bodySchema) obj.bodySchema = stripHTMLFromToolInput(bodySchema)
if (maxOutputLength) obj.maxOutputLength = parseInt(maxOutputLength, 10) if (maxOutputLength) obj.maxOutputLength = parseInt(maxOutputLength, 10)
if (headers) { if (headers) {
const parsedHeaders = typeof headers === 'object' ? headers : JSON.parse(stripHTMLFromToolInput(headers)) const parsedHeaders = typeof headers === 'object' ? headers : JSON5.parse(stripHTMLFromToolInput(headers))
obj.headers = parsedHeaders obj.headers = parsedHeaders
} }
if (body) { if (body) {
const parsedBody = typeof body === 'object' ? body : JSON.parse(body) const parsedBody = typeof body === 'object' ? body : JSON5.parse(body)
obj.body = parsedBody obj.body = parsedBody
} }
@@ -1,6 +1,7 @@
import { z } from 'zod' import { z } from 'zod'
import { DynamicStructuredTool } from '../OpenAPIToolkit/core' import { DynamicStructuredTool } from '../OpenAPIToolkit/core'
import { secureFetch } from '../../../src/httpSecurity' import { secureFetch } from '../../../src/httpSecurity'
import JSON5 from 'json5'
export const desc = `Use this when you want to execute a PUT request to update or replace a resource.` export const desc = `Use this when you want to execute a PUT request to update or replace a resource.`
@@ -27,7 +28,7 @@ const createRequestsPutSchema = (bodySchema?: string) => {
// If bodySchema is provided, parse it and add dynamic body params // If bodySchema is provided, parse it and add dynamic body params
if (bodySchema) { if (bodySchema) {
try { try {
const parsedSchema = JSON.parse(bodySchema) const parsedSchema = JSON5.parse(bodySchema)
const bodyParamsObject: Record<string, z.ZodTypeAny> = {} const bodyParamsObject: Record<string, z.ZodTypeAny> = {}
Object.entries(parsedSchema).forEach(([key, config]: [string, any]) => { Object.entries(parsedSchema).forEach(([key, config]: [string, any]) => {