code icon Code

Merge JSON Files

Merge multiple JSON array files into one

Source Code

import fs from "fs";
import path from "path";

const [inputPathsArg, outputPath] = process.argv.slice(2);

if (!inputPathsArg || !outputPath) {
  console.error("Usage: inputPaths outputPath");
  process.exit(1);
}

const inputPaths = inputPathsArg.split(",").map((p) => p.trim());

try {
  console.log(`Merging ${inputPaths.length} files...`);

  const merged = [];
  const stats = [];

  for (const inputPath of inputPaths) {
    console.log(`  Reading ${inputPath}...`);

    if (!fs.existsSync(inputPath)) {
      console.warn(`  Warning: ${inputPath} not found, skipping`);
      stats.push({ path: inputPath, count: 0, skipped: true });
      continue;
    }

    const raw = fs.readFileSync(inputPath, "utf-8");
    const data = JSON.parse(raw);

    const items = Array.isArray(data)
      ? data
      : data.items || data.results || data.messages || [];

    if (!Array.isArray(items)) {
      console.warn(`  Warning: ${inputPath} is not an array, skipping`);
      stats.push({ path: inputPath, count: 0, skipped: true });
      continue;
    }

    merged.push(...items);
    stats.push({ path: inputPath, count: items.length });
    console.log(`    Added ${items.length} items`);
  }

  // Ensure output directory exists
  const dir = path.dirname(outputPath);
  if (dir && dir !== ".") {
    fs.mkdirSync(dir, { recursive: true });
  }

  fs.writeFileSync(outputPath, JSON.stringify(merged, null, 2));

  console.log(`\nāœ“ Merged ${merged.length} total items from ${inputPaths.length} files`);
  console.log(`  Written to: ${outputPath}`);

  console.log(
    JSON.stringify({
      success: true,
      outputPath,
      totalCount: merged.length,
      sources: stats,
    })
  );
} catch (error) {
  console.error("Error:", error.message);
  process.exit(1);
}