

Your data already tells the story; nested JSON just hides the plot.In Snowflake, most modern customer, product, and event data lands as VARIANT columns full of JSON. That’s perfect for ingestion speed, but painful when a sales leader wants a clean list of accounts, or a marketing manager needs campaign events by day. The FLATTEN table function in Snowflake turns those tangled arrays and objects into tidy rows and columns, so you can query them with standard SQL, join to other tables, and confidently feed models or dashboards.By flattening JSON in Snowflake, you unlock:- Simpler SQL over complex event streams.- Faster reporting and fewer brittle ETL scripts.- Easier exports into tools like Google Sheets for business teams.Now imagine you never again have to click through the console, paste queries, download CSVs, and upload them into Google Sheets. That is where delegation to an AI agent becomes transformative. Instead of a human analyst babysitting every run, an AI agent executes the same Snowflake FLATTEN logic, validates row counts, refreshes your Google Sheets tabs, and notifies stakeholders when it’s done. You get the reliability of a production pipeline with the flexibility of ad‑hoc analysis, and your team finally steps away from the keyboard to work on strategy instead of plumbing.
### OverviewJSON is everywhere in modern data stacks: product events, marketing logs, CRM webhooks. In Snowflake, that usually means big VARIANT columns that are powerful but hard to report on. The FLATTEN table function is your bridge from messy JSON to clean, tabular data you can send straight into Google Sheets. Let’s walk through three layers of maturity:1. Traditional, hands-on ways to flatten JSON in Snowflake.2. No-code automation that runs on a schedule.3. AI-agent-driven workflows that operate like a reliable data analyst you never have to hire.Throughout, keep Snowflake’s FLATTEN docs handy: https://docs.snowflake.com/en/sql-reference/functions/flatten and Google Sheets help here: https://support.google.com/docs.---## 1. Traditional/manual ways to flatten JSON in Snowflake### 1.1 One-off FLATTEN query in Snowflake UI**When to use:** quick exploration, ad-hoc questions.**Steps:**1. **Open Snowsight or Classic UI.** Log in to Snowflake and select the worksheet where you want to work.2. **Inspect the JSON column.** Run `SELECT * FROM your_table LIMIT 10;` and note the VARIANT column (e.g. `payload`).3. **Use FLATTEN as a table function.** For a simple array: ```sql SELECT t.id, f.value AS event FROM your_table t, LATERAL FLATTEN(input => t.payload:events) f; ``` This explodes the `events` array into multiple rows.4. **Target specific paths.** Use `PATH` or direct JSON path access: ```sql SELECT t.id, f.value:event_type::string AS event_type, f.value:timestamp::timestamp AS ts FROM your_table t, LATERAL FLATTEN(input => t.payload:events) f; ```5. **Download results.** In the UI, click **Download** → CSV, then import that file manually into Google Sheets.**Pros:** Fast to start; great for discovery.**Cons:** Manual, error-prone, no scheduling, no direct link to Sheets.---### 1.2 Using OUTER, RECURSIVE, and MODE for tricky JSONWhen JSON is messy or deeply nested, use FLATTEN’s options (see: https://docs.snowflake.com/en/sql-reference/functions/flatten#usage-notes):- `OUTER => TRUE` keeps rows even when the path doesn’t exist, filling NULLs.- `RECURSIVE => TRUE` walks nested structures.- `MODE` controls what to flatten: `OBJECT`, `ARRAY`, or `BOTH`.**Example:**```sqlSELECT t.id, f.path, f.index, f.valueFROM your_table t, LATERAL FLATTEN( input => t.payload, recursive => TRUE, mode => 'BOTH' ) f;```This is helpful when you’re reverse-engineering unfamiliar JSON.---### 1.3 Creating a flattened view**When to use:** repeatable but still human-triggered workflows.1. **Design your flattened schema.** Decide which JSON keys matter to sales, marketing, or ops.2. **Create a view:** ```sql CREATE OR REPLACE VIEW v_events_flat AS SELECT t.id, f.value:event_id::string AS event_id, f.value:user_id::string AS user_id, f.value:campaign::string AS campaign, f.value:created_at::timestamp AS created_at FROM raw.events t, LATERAL FLATTEN(input => t.payload:events) f; ```3. **Query the view whenever needed** and export results manually.**Pros:** Central logic lives in one place; easier to maintain.**Cons:** Still manual exports and refreshes; no built-in Google Sheets sync.---## 2. No-code methods with automation toolsAs your team grows, you don’t want analysts downloading CSVs daily. No-code tools can call Snowflake on a schedule and push results into Google Sheets with minimal engineering.### 2.1 Scheduled queries + external connectorUse a data connector or BI tool that supports Snowflake and Google Sheets. Many let you schedule exports from a SQL query or view.**Typical setup:**1. **Create the FLATTEN-based view** in Snowflake (as above: `v_events_flat`).2. **Connect the tool to Snowflake.** You’ll need account, warehouse, database, schema, and role.3. **Write a query in the tool:** ```sql SELECT * FROM v_events_flat WHERE created_at >= dateadd(day, -7, current_timestamp()); ```4. **Connect Google Sheets.** Choose the target spreadsheet and tab.5. **Set schedule.** For example, refresh every hour or every morning.**Pros:** No code; business teams can manage; Sheets is always current.**Cons:** Limited flexibility around branching logic, QA checks, or multi-step workflows.---### 2.2 Google Sheets data connectorsGoogle provides ways to pull data directly into Sheets.- For general guidance: https://support.google.com/docs/answer/3093342You can:1. Use an add-on that supports Snowflake.2. Configure the SQL (again, usually your flattened view).3. Map column names to Sheet columns.4. Set auto-refresh if the add-on supports it.**Pros:** Lives entirely in Sheets; great for sales or marketing managers.**Cons:** Harder to manage complex JSON logic or multi-table joins outside Snowflake; Sheets becomes the bottleneck for heavy data volumes.---## 3. Automating at scale with AI agentsWhen JSON volumes grow and requirements change weekly, you need something smarter than fixed schedules. This is where an AI agent such as Simular Pro becomes your digital data analyst.Simular Pro agents can:- Log into Snowflake’s UI or your SQL client.- Run and modify FLATTEN-based queries.- Export results and push them into Google Sheets.- Apply checks (row counts, schema validation) before stakeholders see updates.### 3.1 Agent-driven Snowflake → Google Sheets refresh**Workflow concept:** The agent behaves like a trained ops analyst.**Steps:**1. **Record the ideal workflow:** - Open Snowflake. - Run `CREATE OR REPLACE VIEW v_events_flat AS ...` (if not created). - Execute a reporting query against the view. - Export results or copy them via CSV. - Open Google Sheets and paste or upload the data into the correct tab.2. **Teach the agent the goal:** “Every morning at 8am, refresh my campaign-events Sheet from Snowflake using the `v_events_flat` view.”3. **Let the agent handle edge cases:** schema changes, failed queries, or Sheets limits.**Pros:**- No need to wire APIs yourself.- Each step is visible, inspectable, and modifiable.- Agents can handle thousands of UI steps reliably.**Cons:**- Requires an initial onboarding period.- Best results when you clearly define views and queries.---### 3.2 Agents for exploratory JSON analysisBeyond scheduled jobs, AI agents shine when JSON structures evolve.**Example:** You add new event properties to your JSON. Instead of a human spelunking through the structure, the agent can:1. Scan a sample of JSON rows.2. Use `FLATTEN` with `RECURSIVE => TRUE` to map out keys and paths.3. Propose an updated flattened schema.4. Generate and run a migration script to extend your view.This turns what used to be a half-day of trial-and-error into a few supervised agent runs.---### 3.3 Choosing your approach- **If you just need occasional exports:** manual FLATTEN queries plus CSV are enough.- **If business teams need daily metrics:** create flattened views and connect them to Google Sheets with no-code tools.- **If JSON schemas change and stakeholders expect always-on reporting:** delegate the entire Snowflake → Google Sheets workflow to an AI agent that understands both the SQL and the surrounding clicks.Start at the layer that fits today, but design your Snowflake FLATTEN logic (views and queries) so an AI agent can take over tomorrow without rewriting everything.
In Snowflake, FLATTEN is a table function that explodes semi-structured data (VARIANT, OBJECT, ARRAY) into multiple rows so you can query it like a normal table. Use it when your data is stored as JSON but your questions are relational: “Show me one row per event”, “Give me each order item”, or “List every property on this object.”The basic syntax is:```sqlSELECT *FROM TABLE(FLATTEN(input => your_variant_column));```You often combine FLATTEN with a base table using a LATERAL join:```sqlSELECT t.id, f.valueFROM raw.events t, LATERAL FLATTEN(input => t.payload:events) f;```This returns one row for each array element under `payload:events`. From there, you cast out fields with `f.value:key::type`. Full details are in the docs: https://docs.snowflake.com/en/sql-reference/functions/flatten.
To flatten nested JSON in Snowflake, you chain multiple FLATTEN calls using LATERAL joins, each targeting a deeper level in the structure. Start with the outer object or array, then step into nested arrays.Example: customer JSON with an `orders` array:```sqlSELECT c.customer_id, f.value:name::string AS name, o.value:order_id::int AS order_id, o.value:price::number AS priceFROM customer_data c, LATERAL FLATTEN(input => c.details) f, LATERAL FLATTEN(input => f.value:orders) o;```Here, the first FLATTEN unwraps the root JSON object; the second FLATTEN explodes the `orders` array. For deeper nesting, add more FLATTEN steps or use `RECURSIVE => TRUE` when exploring structures. See examples in Snowflake’s docs: https://docs.snowflake.com/en/user-guide/querying-semistructured.
By default, FLATTEN drops rows where the target path cannot be expanded or has zero entries. To preserve those rows, use the `OUTER => TRUE` option. This tells Snowflake to emit one row even when the expansion is empty, filling FLATTEN’s KEY, INDEX, and VALUE columns with NULL.Example:```sqlSELECT t.id, f.valueFROM raw.events t, LATERAL FLATTEN( input => t.payload:events, outer => TRUE ) f;```This is critical for accurate reporting: you won’t silently lose customers or sessions that happen to have no events. Instead, you’ll see them as rows with NULL event values and can handle them in SQL (e.g. `COALESCE`, `LEFT JOIN`). The behavior is described under OUTER in the FLATTEN reference: https://docs.snowflake.com/en/sql-reference/functions/flatten#arguments.
First, isolate your FLATTEN logic in a stable view, then connect that view to Google Sheets. In Snowflake:```sqlCREATE OR REPLACE VIEW v_events_flat ASSELECT t.id, f.value:event_type::string AS event_type, f.value:ts::timestamp AS tsFROM raw.events t, LATERAL FLATTEN(input => t.payload:events) f;```Now you have a clean, tabular interface. To get this into Google Sheets, you can:1. Use a connector or add-on that supports Snowflake.2. Point it to `SELECT * FROM v_events_flat`.3. Map columns to your target Sheet and schedule refreshes.Alternatively, export a CSV from Snowflake UI and import it manually into Sheets (File → Import). For help on importing data, see: https://support.google.com/docs/answer/40608.
An AI agent can act like a tireless data ops assistant. Instead of you logging into Snowflake, running FLATTEN queries, exporting results, and updating Google Sheets, the agent does all of that step by step.A typical workflow looks like this:1. You define or refine a view in Snowflake that uses FLATTEN to expose clean columns.2. You show the agent how to open Snowflake, run the query, verify row counts, and handle simple errors.3. The agent exports the results and updates the correct Sheet tab, preserving headers and formats.4. You schedule the agent or trigger it via webhook whenever new data arrives.Because each action is visible and modifiable, you can audit and improve the workflow over time. This moves your team from manual, fragile routines to a robust, AI-run pipeline that still uses standard Snowflake FLATTEN under the hood.