# JSON Flattener - Nested JSON to Flat CSV or JSON (`kotaro-project/json-flattener`) Actor

Turn nested JSON from a URL or pasted text into flat rows: one row per record, nested keys become columns like address.city. Choose the separator and how arrays are handled, pick the records path, get CSV or flat JSON with a download link. JSON Lines works too. No login, no API key.

- **URL**: https://apify.com/kotaro-project/json-flattener.md
- **Developed by:** [KOTARO project](https://apify.com/kotaro-project) (community)
- **Features:** Limited permissions
- **Stats:** 2 total users, 1 monthly users, 100.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

from $2.00 / 1,000 files converted

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/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

## JSON Flattener - Nested JSON to Flat CSV or JSON

Turn nested JSON into flat rows you can open in Excel, Google Sheets or a database. Each record becomes one row, and nested
keys become column names such as `address.city` or `company.name`.

### What it does

- Reads JSON from a **public URL** or **pasted text**. JSON Lines (one object per line) works too.
- Finds the list of records automatically (a top-level list, or the first list of objects one level down such as `{"data": [...]}`),
  or uses the **records path** you give (`data.items`, `results.0.rows`).
- Joins nested keys with the **separator** you choose (`.` or `_` or anything else).
- Handles **arrays** your way:
  - **index**: one column per item (`tags.0`, `tags.1`)
  - **join**: simple lists in one cell, separated by `; ` (lists of objects stay indexed)
  - **json**: the whole list as JSON text in one cell
- Writes **CSV** (UTF-8, comma / semicolon / tab / pipe) or a **flat JSON** list, and gives you a **download link**.
- Invalid JSON gets a clear message with the **line and column** of the problem.

### Input example

```json
{
  "jsonUrl": "https://jsonplaceholder.typicode.com/users",
  "separator": ".",
  "arrays": "index",
  "outputFormat": "csv"
}
```

### Output example

```json
{
  "fileName": "flattened.csv",
  "downloadUrl": "https://api.apify.com/v2/key-value-stores/<store>/records/flattened.csv",
  "format": "csv",
  "records": 10,
  "columnCount": 15,
  "columns": ["id", "name", "username", "email", "address.street", "address.city", "address.geo.lat", "company.name"],
  "sample": [{"id": 1, "name": "Leanne Graham", "address.city": "Gwenborough"}]
}
```

### Pricing

One `file-converted` event per converted file. An empty input returns a free note. If the file cannot be downloaded or is not valid
JSON, you get an error row and are not charged.

### Limits and safety

- Files up to 50 MB, up to 500,000 records and 5,000 columns per run.
- Only public `http`/`https` addresses on the standard ports. Internal and private network addresses are refused.
- No login, no API key, nothing is stored outside your own Apify storage.
- Only use files you are allowed to process.

# Actor input Schema

## `jsonUrl` (type: `string`):

A public link to a JSON or JSON Lines file (up to 50 MB). Leave empty to use the pasted text below.

## `jsonText` (type: `string`):

Used when no URL is given. A list of objects, one object, or JSON Lines (one object per line).

## `recordsPath` (type: `string`):

Where the list of records is, as a dotted path such as data.items or results.0.rows. Leave empty to find it automatically (a top-level list, or the first list of objects one level down).

## `separator` (type: `string`):

Joins nested keys into one column name, e.g. "." gives address.city and "\_" gives address_city.

## `arrays` (type: `string`):

Index makes one column per item (tags.0, tags.1). Join puts simple lists in one cell separated by "; " (lists of objects stay indexed). JSON keeps each list as JSON text in one cell.

## `outputFormat` (type: `string`):

CSV (UTF-8) or a flat JSON list of objects.

## `csvDelimiter` (type: `string`):

Used only for CSV output.

## Actor input object example

```json
{
  "jsonUrl": "https://jsonplaceholder.typicode.com/users",
  "jsonText": "[{\"id\": 1, \"name\": \"Mika\", \"address\": {\"city\": \"Tokyo\", \"zip\": \"100-0001\"}, \"tags\": [\"new\", \"vip\"]},\n {\"id\": 2, \"name\": \"Ken\", \"address\": {\"city\": \"Osaka\"}, \"tags\": []}]",
  "separator": ".",
  "arrays": "index",
  "outputFormat": "csv",
  "csvDelimiter": "comma"
}
```

# Actor output Schema

## `results` (type: `string`):

No description

# 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 = {
    "jsonUrl": "https://jsonplaceholder.typicode.com/users",
    "jsonText": `[{"id": 1, "name": "Mika", "address": {"city": "Tokyo", "zip": "100-0001"}, "tags": ["new", "vip"]},
 {"id": 2, "name": "Ken", "address": {"city": "Osaka"}, "tags": []}]`
};

// Run the Actor and wait for it to finish
const run = await client.actor("kotaro-project/json-flattener").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 = {
    "jsonUrl": "https://jsonplaceholder.typicode.com/users",
    "jsonText": """[{\"id\": 1, \"name\": \"Mika\", \"address\": {\"city\": \"Tokyo\", \"zip\": \"100-0001\"}, \"tags\": [\"new\", \"vip\"]},
 {\"id\": 2, \"name\": \"Ken\", \"address\": {\"city\": \"Osaka\"}, \"tags\": []}]""",
}

# Run the Actor and wait for it to finish
run = client.actor("kotaro-project/json-flattener").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 '{
  "jsonUrl": "https://jsonplaceholder.typicode.com/users",
  "jsonText": "[{\\"id\\": 1, \\"name\\": \\"Mika\\", \\"address\\": {\\"city\\": \\"Tokyo\\", \\"zip\\": \\"100-0001\\"}, \\"tags\\": [\\"new\\", \\"vip\\"]},\\n {\\"id\\": 2, \\"name\\": \\"Ken\\", \\"address\\": {\\"city\\": \\"Osaka\\"}, \\"tags\\": []}]"
}' |
apify call kotaro-project/json-flattener --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,kotaro-project/json-flattener"
        }
    }
}
```

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/BXvTXwlpIeYjcRyJQ/builds/3HbzKczqoQvwY8MCZ/openapi.json
