merge
Combine multiple JSON files from the specified directory path into a single JSON file.
Instructions
Merge JSON files into a one JSON file
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Implementation Reference
- src/index.ts:147-153 (registration)Tool 'merge' registered in ListToolsRequestSchema handler with name 'merge' and description 'Merge JSON files into a one JSON file'. Input schema uses PathArgSchema.
{ name: 'merge', description: 'Merge JSON files into a one JSON file', inputSchema: zodToJsonSchema(PathArgSchema) as ToolInput, }, ], } - src/index.ts:39-41 (schema)PathArgSchema defines the schema for the 'merge' tool: expects a single 'path' string argument.
const PathArgSchema = z.object({ path: z.string(), }) - src/index.ts:210-239 (handler)Handler for 'merge' tool: parses the path argument, validates the path, reads all JSON files in the directory via loadFlatJsons, flattens their contents, and writes the merged result to 'merged.json' in the same directory.
case 'merge': { const parsed = PathArgSchema.safeParse(args) if (!parsed.success) { throw new Error(`Invalid arguments for merge: ${parsed.error}`) } const { path } = parsed.data const validPath = await validatePath(path) const content = await loadFlatJsons(validPath) const fileName = _path.join(validPath, 'merged.json') try { await fs.promises.writeFile( fileName, JSON.stringify(content, null, 2) ) console.log(`Successfully wrote ${fileName}`) } catch (err) { console.error(`Error writing file ${fileName}:`, err) } return { content: [ { type: 'text', text: `Successfully merged to ${fileName}`, }, ], } } - src/index.ts:101-136 (helper)Helper functions used by merge: getAllJsonNames, getMultipleJsonFilePath, readFileToJson, parseMultipleJson, and loadFlatJsons - these collectively read all .json files from a directory and return their flattened combined content.
const getAllJsonNames = async (dir: string): Promise<string[]> => getAllFileNames(dir, '.json') const getMultipleJsonFilePath = async (fileDir: string): Promise<string[]> => { const filePaths: string[] = [] const fileNames = await getAllJsonNames(fileDir) for (let index = 0; index < fileNames.length; index++) { filePaths.push(`${fileDir}/${fileNames[index]}`) } return filePaths } const readFileToJson = async (fileName: string): Promise<any[]> => { try { const content = await fs.promises.readFile(fileName, 'utf-8') return JSON.parse(content) } catch (error) { throw new Error(`Error reading or parsing file ${fileName}: ${error}`) } } const parseMultipleJson = async (fileDir: string) => { const multipleJson: any = [] const fileNames = await getMultipleJsonFilePath(fileDir) for (let index = 0; index < fileNames.length; index++) { multipleJson.push(readFileToJson(fileNames[index])) } return multipleJson } const loadFlatJsons = async (path: string) => { const promises = await parseMultipleJson(path) if (promises.length === 0) throw new Error(`No files in ${path}`) const jsons = await Promise.all(promises) return jsons.flat() } - src/index.ts:56-73 (helper)validatePath helper used by merge to resolve and validate the provided directory path.
const validatePath = async (requestedPath: string): Promise<string> => { const expandedPath = expandHome(requestedPath) const absolute = _path.isAbsolute(expandedPath) ? _path.resolve(expandedPath) : _path.resolve(process.cwd(), expandedPath) try { const realPath = await fs.promises.realpath(absolute) return realPath } catch (error) { const parentDir = _path.dirname(absolute) try { return absolute } catch { throw new Error(`Parent directory does not exist: ${parentDir}`) } } }