Skip to main content
Glama

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
NameRequiredDescriptionDefault
templateYesJSON template with placeholders
textYesGroupped text from groupTextByJson tool

Implementation Reference

  • 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}`); } };
  • 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), },
  • 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);
  • Constant defining the tool name 'text-to-json' used in registration and dispatching.
    textToJson: "text-to-json",

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/omer-ayhan/custom-context-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server