> ## Documentation Index
> Fetch the complete documentation index at: https://docs.prophecy.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# FlattenSchema gem for Data Engineering

> Flatten nested data

<Panel>
  <Info>
    Dependencies:

    * ProphecySparkBasicsPython 0.0.1+
    * ProphecySparkBasicsScala 0.0.1+
  </Info>

  <Info>
    Cluster requirements:

    * UC dedicated clusters 14.3+ supported
    * UC standard clusters 14.3+ supported
    * Livy clusters 3.0.1+ supported
  </Info>
</Panel>

When processing raw data it can be useful to flatten complex data types like structures and arrays into simpler, flatter schemas.

<img src="https://mintcdn.com/prophecy-62973bd0/YMU5yAViYYX3rOGW/data-engineering/gems/transform/img/flatten_gem.png?fit=max&auto=format&n=YMU5yAViYYX3rOGW&q=85&s=289f60c7df69597698c4463e35727350" alt="The FlattenSchema gem" width="290" height="305" data-path="data-engineering/gems/transform/img/flatten_gem.png" />

## The Input

FlattenSchema works on DataFrames that have nested columns that you'd like to extract into a flat schema.

For example, with an input schema like so:

<img src="https://mintcdn.com/prophecy-62973bd0/YMU5yAViYYX3rOGW/data-engineering/gems/transform/img/flatten_input.png?fit=max&auto=format&n=YMU5yAViYYX3rOGW&q=85&s=8b0cb86a01347de8ec53ba68c11944d0" alt="Input schema" width="376" height="434" data-path="data-engineering/gems/transform/img/flatten_input.png" />

And the data looks like so:

<img src="https://mintcdn.com/prophecy-62973bd0/YMU5yAViYYX3rOGW/data-engineering/gems/transform/img/flatten_input_interim.png?fit=max&auto=format&n=YMU5yAViYYX3rOGW&q=85&s=7e76220f6826e101165d07526f9efe98" alt="Input data" width="2764" height="416" data-path="data-engineering/gems/transform/img/flatten_input_interim.png" />

We want to extract `count` from *result* and all of the columns from *events* into a flattened schema.

## The Expressions

Having added a FlattenSchema gem to your pipeline, all you need to do is click the column names you wish to extract and they'll be added to the **Expressions** section. Then, you can change the values in the **Target Column** to change the name of output columns.

<img src="https://mintcdn.com/prophecy-62973bd0/YMU5yAViYYX3rOGW/data-engineering/gems/transform/img/flatten_add_exp.gif?s=58e4bdad3131b8363716431867a309bb" alt="Adding Expressions" width="630" height="432" data-path="data-engineering/gems/transform/img/flatten_add_exp.gif" />

The **Columns Delimiter** dropdown allows you to control how the names of the new columns are derived. Currently dashes and underscores are supported.

## The Output

If we check the **Output** tab in the gem, you'll see the schema that we've created using the selected columns.

<img src="https://mintcdn.com/prophecy-62973bd0/YMU5yAViYYX3rOGW/data-engineering/gems/transform/img/flatten_output.png?fit=max&auto=format&n=YMU5yAViYYX3rOGW&q=85&s=a36a2c666fdc7fd71261b89f45abe290" alt="Output schema" width="404" height="531" data-path="data-engineering/gems/transform/img/flatten_output.png" />

And here's what the output data looks like:

<img src="https://mintcdn.com/prophecy-62973bd0/5tvw_2e98LqB5rz7/data-engineering/gems/transform/img/flatten_output_interim.png?fit=max&auto=format&n=5tvw_2e98LqB5rz7&q=85&s=00b4d26a8970a634b328f6268e7eb888" alt="Output interim" width="2766" height="1436" data-path="data-engineering/gems/transform/img/flatten_output_interim.png" />

No more nested structures!

<Info>
  For more advanced use cases, the Spark `explode` function is available to use in the
  [Reformat](./reformat.md) gem, [Custom SQL](../custom/sql-statement.md) gem, or anywhere else that
  accepts Spark expressions.
</Info>
