# Merge, Dedupe & Transform Datasets (`brightledger/merge-dedupe-datasets`) Actor

Combine several Apify datasets into one clean dataset: remove duplicates by any field, keep first or newest copy, flatten nested data, pick, drop or rename columns. Pay only for rows written.

- **URL**: https://apify.com/brightledger/merge-dedupe-datasets.md
- **Developed by:** [BrightLedger](https://apify.com/brightledger) (community)
- **Security:** Limited permissions
- **Stats:** 2 total users, 1 monthly users, 100.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

from $0.20 / 1,000 rows

This Actor is paid per event. You are not charged for the Apify platform usage, but only a fixed price for specific events.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-event

## What's an Apify Actor?

An Actor is a serverless cloud program that runs on the Apify platform. It has two run modes.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.

Apify vocabulary and the platform model are defined once, in the agent quickstart at https://apify.com/agents.md.

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.

Do not guess an integration path. Every one of them is in the agent quickstart at https://apify.com/agents.md: the Apify MCP server, Agent Skills with the Apify CLI, the JavaScript and Python clients, the REST API, and the account-free path for an agent with no human to sign in. It also carries the rule on stating cost before the first paid run.

For examples already wired to this Actor's own input schema, see the [API](#api) section below.

Each client library has reference documentation the quickstart does not restate: [JavaScript/TypeScript](https://docs.apify.com/api/client/js/docs.md) (`npm install apify-client`) and [Python](https://docs.apify.com/api/client/python/docs.md) (`pip install apify-client`).

# README

## Merge, Dedupe & Transform Datasets

Combine the results of several runs into **one clean dataset**. Remove duplicates by any field, keep the first or newest copy, flatten nested data, and pick, drop, or rename columns.

### Common uses

- **Combine daily or weekly scrapes** into one list with no repeats (dedupe by `url`).
- **Merge lead lists from different tools** and remove duplicate companies (dedupe by `domain`) or people (dedupe by `email`).
- **Keep the freshest data**: choose *Last seen* to keep the newest copy of each record in its original position.
- **Prepare for export**: flatten nested fields and keep only the columns you need before sending to CSV, Excel, or Google Sheets.

### How matching works

- Duplicates are matched on the fields you list, across all datasets, in the order you give them.
- By default matching ignores case and extra spaces (`Acme.com` = `acme.com `).
- Rows where all key fields are empty are kept as they are, never merged into one.
- Nested fields can be used as keys with dots, e.g. `socials.linkedin`.

### Input tips

Run outputs are unnamed datasets, so they may not show in the dataset picker. Switch the input to **JSON** and paste their IDs (open a run, then the **Storage** tab, then copy the **Dataset ID**):

```json
{
  "datasetIds": ["FIRST_DATASET_ID", "SECOND_DATASET_ID"],
  "dedupeFields": ["domain"],
  "keep": "first"
}
```

### Output

- The combined rows in this run's dataset, with an optional `_sourceDatasetId` column.
- A summary record: items read per dataset, duplicates removed, rows written.

### Pricing

You pay per row written to the output. Removed duplicates are free.

### Works well with

- **Company Website to AI Company Profile** (brightledger/company-website-profiler): merge profiles from several runs, dedupe by `domain`.
- **Google Sheets Export** (brightledger/google-sheets-export): send the clean result to your sheet.

# Actor input Schema

## `datasetIds` (type: `array`):

The datasets to combine, in order. Run outputs are unnamed and may not appear in the list: switch to the JSON tab and paste their IDs (open a run > Storage tab > Dataset ID). Without any dataset, the run just explains the setup.

## `dedupeFields` (type: `array`):

Field(s) that identify the same record, e.g. url, domain, or email. Nested fields use dots, e.g. socials.linkedin. Leave empty to only merge.

## `keep` (type: `string`):

When the same record appears more than once.

## `caseInsensitive` (type: `boolean`):

Treats Acme.com and acme.com as the same record.

## `flatten` (type: `boolean`):

Turns nested objects into columns like socials.linkedin and lists into comma-separated text. Handy before exporting to CSV or Sheets.

## `fields` (type: `array`):

Leave empty to keep everything. Listed columns come out in this order.

## `excludeFields` (type: `array`):

Columns to remove from the output.

## `rename` (type: `object`):

Map old names to new ones, e.g. {"companyName": "Company"}.

## `addSourceField` (type: `boolean`):

Adds \_sourceDatasetId so you can see where each row came from.

## `maxItems` (type: `integer`):

Stop after reading this many items across all datasets. 0 means no limit.

## Actor input object example

```json
{
  "keep": "first",
  "caseInsensitive": true,
  "flatten": false,
  "rename": {},
  "addSourceField": true,
  "maxItems": 0
}
```

# Actor output Schema

## `rows` (type: `string`):

The merged, deduplicated dataset.

## `summary` (type: `string`):

Items read per dataset, duplicates removed, rows written.

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "rename": {}
};

// Run the Actor and wait for it to finish
const run = await client.actor("brightledger/merge-dedupe-datasets").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = { "rename": {} }

# Run the Actor and wait for it to finish
run = client.actor("brightledger/merge-dedupe-datasets").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "rename": {}
}' |
apify call brightledger/merge-dedupe-datasets --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,brightledger/merge-dedupe-datasets"
        }
    }
}
```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/RHQKEWuSZRyordgUp/builds/SUVVWi4QUYofJmh5h/openapi.json
