Skip to main content
Glama

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
NameRequiredDescriptionDefault
pathYes

Implementation Reference

  • 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}`, }, ], } }
  • 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, },
  • 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() }

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/VadimNastoyashchy/json-mcp'

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