json_validate
Validate JSON strings to detect parsing errors and identify their approximate locations for debugging and data integrity.
Instructions
Validate a JSON string and report any parsing errors with their approximate location.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input | Yes | The JSON string to validate |
Implementation Reference
- src/tools/formatters.ts:42-94 (handler)The implementation of the json_validate tool, which validates JSON strings and returns either a success message or an error with position and context.
server.tool( "json_validate", "Validate a JSON string and report any parsing errors with their approximate location.", { input: z.string().describe("The JSON string to validate") }, async ({ input }) => { try { JSON.parse(input); return { content: [ { type: "text" as const, text: JSON.stringify( { valid: true, message: "Valid JSON" }, null, 2 ), }, ], }; } catch (e) { const message = e instanceof Error ? e.message : String(e); // Try to extract position info from error const posMatch = message.match(/position\s+(\d+)/i); const position = posMatch ? parseInt(posMatch[1], 10) : undefined; let context: string | undefined; if (position !== undefined) { const start = Math.max(0, position - 20); const end = Math.min(input.length, position + 20); context = input.slice(start, end); } return { content: [ { type: "text" as const, text: JSON.stringify( { valid: false, error: message, position, context, }, null, 2 ), }, ], }; } } );