> For the complete documentation index, see [llms.txt](https://upsolver.gitbook.io/content/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://upsolver.gitbook.io/content/articles-1/jobs/transformation/flattening-arrays.md).

# Flattening Arrays

The `FLATTEN_PATHS` feature enables you to flatten the results of a query based on the provided paths. This feature can transform nested arrays within your data into separate rows in the output, providing a simple and efficient way to handle hierarchical data structures.

## Syntax

The basic syntax to use `FLATTEN_PATHS` in a transformation job is as follows:

```sql
CREATE sync JOB job_name
    FLATTEN_PATHS = (path1, path2, ...)
AS INSERT INTO target_table
      SELECT *
      FROM source_table;
```

{% hint style="info" %}
The `FLATTEN_PATHS` feature is currently only supported when writing to Upsolver tables.
{% endhint %}

## Example: Flattening a Single Path

The following example demonstrates how to flatten the `items` array.

### SQL Code

```sql
CREATE sync JOB flatten_single_path_example
    FLATTEN_PATHS = (items)
AS INSERT INTO orders_database.flattened_orders
       SELECT *
       FROM orders_database.raw_orders;
```

### Input Row

```json
{
  "name": "Order1",
  "items": [
    {"product": "apple", "quantity": 3, "categories": ["fruit", "fresh"]},
    {"product": "sugar", "quantity": 2, "categories": ["pantry"]}
  ]
}
```

### Output Rows

```json
{
  "name": "Order1",
  "product": "apple",
  "quantity": 3,
  "categories": ["fruit", "fresh"]
}
```

```json
{
  "name": "Order1",
  "product": "sugar",
  "quantity": 2,
  "categories": ["pantry"]
}
```

## Example: Flattening a Nested Array

### SQL Code

```sql
CREATE sync JOB flatten_nested_path_example
    FLATTEN_PATHS = (items[].categories)
AS INSERT INTO orders_database.flattened_categories
      SELECT *
      FROM orders_database.raw_orders;
```

### Input Row

```json
{
  "name": "Order1",
  "items": [
    {"product": "apple", "quantity": 3, "categories": ["fruit", "fresh"]},
    {"product": "sugar", "quantity": 2, "categories": ["pantry"]}
  ]
}
```

### Output Rows

```json
{
  "name": "Order1",
  "product": "apple",
  "quantity": 3,
  "categories": "fruit"
}
```

```json
{
  "name": "Order1",
  "product": "apple",
  "quantity": 3,
  "categories": "fresh"
}
```

```json
{
  "name": "Order1",
  "product": "sugar",
  "quantity": 2,
  "categories": "pantry"
}
```

## Example: Flattening Multiple Paths Creating a Cartesian Product

In this example, we flatten both `categories` and `pricing`, resulting in a Cartesian product since two independent arrays are used in the flattening.

### SQL Code

```sql
CREATE sync JOB flatten_multiple_paths_example
    FLATTEN_PATHS = (categories, pricing)
AS INSERT INTO products_database.flattened_products
      SELECT *
      FROM products_database.raw_products;
```

### Input Row

```json
{
  "name": "Apple",
  "categories": ["fruit", "fresh"],
  "pricing": [ {"members": 2.99}, {"general": 3.99}]
}
```

### Output Rows

Here, flattening both `categories` and `pricing` creates a Cartesian product of the two arrays, leading to combinations of each category with each pricing option.

```json
{
  "name": "Apple",
  "categories": "fruit",
  "pricing": {"members": 2.99}
}
```

```json
{
  "name": "Apple",
  "categories": "fruit",
  "pricing": {"general": 3.99}
}
```

```json
{
  "name": "Apple",
  "categories": "fresh",
  "pricing": {"members": 2.99}
}
```

```json
{
  "name": "Apple",
  "categories": "fresh",
  "pricing": {"general": 3.99}
}
```

The `FLATTEN_PATHS` feature provides an intuitive way to handle complex data structures, making it easier to work with nested arrays in your data processing tasks.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://upsolver.gitbook.io/content/articles-1/jobs/transformation/flattening-arrays.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
