merge
Combine multiple JSON files into a single file by specifying the path, streamlining data integration and management within the json-mcp-server environment.
Instructions
Merge JSON files into a one JSON file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Implementation Reference
- src/index.ts:210-239 (handler)Handler for the 'merge' tool: parses arguments, validates and expands path to directory, loads all JSON files in the directory, flattens and merges their contents, writes to 'merged.json' in the same directory, returns success message.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:39-41 (schema)Zod input schema for the 'merge' tool: requires a single 'path' string argument pointing to the directory containing JSON files to merge.const PathArgSchema = z.object({ path: z.string(), })
- src/index.ts:147-151 (registration)Registration of the 'merge' tool in the ListTools response, including name, description, and input schema reference.{ name: 'merge', description: 'Merge JSON files into a one JSON file', inputSchema: zodToJsonSchema(PathArgSchema) as ToolInput, },
- src/index.ts:131-136 (helper)Key helper function used by merge handler: asynchronously loads all JSON files in the given directory path, parses them, and returns a flat array of all objects.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() }