text-to-json
Transform grouped text into structured JSON using customizable templates with placeholders, enabling easy data organization and integration into workflows.
Instructions
Converts groupped text from group-text-by-json tool to JSON. This tool accepts a JSON template with placeholders and groupped text from group-text-by-json tool.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| template | Yes | JSON template with placeholders | |
| text | Yes | Groupped text from groupTextByJson tool |
Implementation Reference
- src/common/tools.ts:55-86 (handler)The core handler function that processes the template and text inputs, extracts key-value pairs using helper functions, constructs a prompt with the JSON result, and returns it as MCP content.const textToJsonTool = (template: string, text: string) => { if (!template || !text) { throw new Error("Both template and text are required"); } try { const templateObj = JSON.parse(template); const templateKeys = deepObjectKeys(templateObj, true); const jsonResult = extractKeyValuesFromText(text, templateKeys); const resultPrompt = ` Print this JSON result in JSON format. JSON result: ${JSON.stringify(jsonResult)} `; return { content: [ { type: "text", text: resultPrompt, }, ], }; } catch (error) { logger.error("Error processing template:", error); throw new Error(`Failed to process template: ${error}`); } };
- src/common/types.ts:6-9 (schema)Zod schema defining the input parameters for the text-to-json tool: template (JSON template string) and text (grouped text string).const TextToJsonSchema = z.object({ template: z.string().describe("JSON template with placeholders"), text: z.string().describe("Groupped text from groupTextByJson tool"), });
- src/index.ts:45-49 (registration)Tool registration in the ListTools response, specifying name, description, and input schema derived from Zod schema.{ name: TOOL_NAMES.textToJson, description: `Converts groupped text from ${TOOL_NAMES.groupTextByJson} tool to JSON. This tool accepts a JSON template with placeholders and groupped text from ${TOOL_NAMES.groupTextByJson} tool.`, inputSchema: zodToJsonSchema(TextToJsonSchema), },
- src/index.ts:69-71 (handler)Dispatcher case in the CallTool request handler that validates args and invokes the textToJsonTool handler.case TOOL_NAMES.textToJson: const textToJsonArgs = args as TextToJsonSchemaType; return textToJsonTool(textToJsonArgs.template, textToJsonArgs.text);
- src/common/constants.ts:8-8 (helper)Constant defining the tool name 'text-to-json' used in registration and dispatching.textToJson: "text-to-json",