json-to-csv
Convert JSON strings to CSV format with customizable delimiters using this utility from the IT Tools MCP Server, simplifying data transformation for developers and administrators.
Instructions
Convert JSON to CSV format
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| delimiter | No | CSV delimiter | |
| json | Yes | JSON string to convert to CSV |
Implementation Reference
- The handler function for the 'convert_json_to_csv' tool. It parses the input JSON (expecting an array of objects), uses PapaParse to convert it to CSV with the specified delimiter, and returns the CSV content along with a summary, or an error message if conversion fails.}, async ({ json, delimiter = "," }) => { try { const Papa = (await import("papaparse")).default; const data = JSON.parse(json); if (!Array.isArray(data)) { throw new Error("JSON must be an array of objects"); } const csv = Papa.unparse(data, { delimiter }); return { content: [ { type: "text", text: `CSV:\n${csv}\n\nConversion Summary:\nRows: ${data.length}\nDelimiter: \"${delimiter}\"`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error converting JSON to CSV: ${error instanceof Error ? error.message : 'Unknown error'}`, }, ], }; } }
- The input schema for the tool, validating the 'json' parameter as a string and 'delimiter' as an optional string.inputSchema: { json: z.string().describe("JSON string to convert to CSV"), delimiter: z.string().describe("CSV delimiter").optional(), },
- src/tools/data_format/convert_json_to_csv/index.ts:4-45 (registration)The registration function 'registerConvertJsonCsv' that registers the 'convert_json_to_csv' tool on the MCP server, including description, schema, annotations, and the inline handler.export function registerConvertJsonCsv(server: McpServer) { server.registerTool("convert_json_to_csv", { description: "Convert JSON to CSV format", inputSchema: { json: z.string().describe("JSON string to convert to CSV"), delimiter: z.string().describe("CSV delimiter").optional(), }, // VS Code compliance annotations annotations: { title: "Convert Json To Csv", description: "Convert JSON to CSV format", readOnlyHint: false } }, async ({ json, delimiter = "," }) => { try { const Papa = (await import("papaparse")).default; const data = JSON.parse(json); if (!Array.isArray(data)) { throw new Error("JSON must be an array of objects"); } const csv = Papa.unparse(data, { delimiter }); return { content: [ { type: "text", text: `CSV:\n${csv}\n\nConversion Summary:\nRows: ${data.length}\nDelimiter: \"${delimiter}\"`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error converting JSON to CSV: ${error instanceof Error ? error.message : 'Unknown error'}`, }, ], }; } } ); }