What you will be able to do
- Store whole JSON records in a VARIANT column and query nested fields and array elements with path syntax
- Load JSON into VARIANT with Auto Loader's singleVariantColumn option or with COPY INTO
- Turn on typed inference for JSON with cloudFiles.inferColumnTypes, and pin individual nested fields with schema hints
- Pick the schemaEvolutionMode that keeps a JSON stream running and sends unexpected fields to _rescued_data
Key concept
VARIANT column — A Delta column type that holds a whole semi-structured record, such as a nested JSON object with its arrays, as one value. You read fields out of it with path expressions when you query, so you don't have to flatten the record or fix a rigid schema when you load it.
1.Storing a whole JSON record in a VARIANT column
Nested JSON is a poor fit for a fixed set of columns. Event payloads gain fields, a single key can hold a number in one file and a string in the next, and arrays of objects sit three levels down. You can flatten all of this into a rigid schema when you load it, but every change in the source then becomes a change to your table. The alternative is a VARIANT column. It stores each record whole, and you pull fields out of it when you query. VARIANT is a standard SQL type from Databricks Runtime 15.3 and works in Delta tables. That means it works in Unity Catalog managed tables, which use Delta by default.
Support depends on the file format. JSON can be ingested as VARIANT from Databricks Runtime 15.3. XML and CSV need 16.4 or later. A table can be a single VARIANT column, CREATE TABLE table_name (variant_column VARIANT). It can also be built with CTAS from a column of JSON strings: PARSE_JSON turns each string into a VARIANT, and a path expression pulls a field out as an ordinary column next to it.
CREATE TABLE table_name AS
SELECT json_string:id AS id,
PARSE_JSON(json_string) variant_column
FROM source_dataPulling id out like this is the recommended pattern. Fields that queries filter or join on most often should become their own non-variant columns, because that speeds up queries and gives better storage layout. There is also a hard reason to do it. A VARIANT column cannot be a clustering key, a partition column or a Z-order key, and the type cannot be used in comparisons, GROUP BY, ORDER BY or set operations. If a dashboard groups by event_type, then event_type has to be a real column.
Checkpoint 1 of 9· Fill the gap
This CTAS keeps each full JSON record as a VARIANT and also pulls id out into its own column. Which function completes it?
CREATE TABLE table_name AS
SELECT json_string:id AS id,
? (json_string) variant_column
FROM source_dataPARSE_JSON turns each JSON string into a VARIANT value, so variant_column holds the whole record. FROM_XML is the function for XML strings, and TO_JSON goes the other way, producing a JSON string.
Source: docs.databricks.comCheckpoint 2 of 9· Check yourself
A Delta table in Unity Catalog stores each JSON event in a VARIANT column called payload. Dashboards filter and group by event_type, a field inside payload. How should you lay out the table so these queries run efficiently?
A VARIANT column can't be a clustering key, partition column or Z-order key, and it can't be used for grouping either. A field that queries filter or group on has to become a real column, and you cluster on that column.
“VARIANT columns cannot be used for clustering keys, partitions, or Z-order keys.”Source: docs.databricks.com
Sources1
2.Querying nested fields with path syntax
You query a VARIANT with the same path operators used on JSON strings:
:selects a top-level field, as inraw:owner..or['key']steps into a nested object, as inraw:store.bicycle.[n]picks an array element by 0-based index, as inraw:store.fruit[0].
A path that matches nothing returns NULL rather than an error. The same is true of an array index that is out of range.
-- Use dot notation
SELECT raw:store.bicycle FROM store_data
-- Use brackets
SELECT raw:store['bicycle'] FROM store_dataA path into a VARIANT returns another VARIANT. To get a typed value, cast it with ::, for example raw:store.bicycle.price::double. To turn a nested object or array into a struct or array type, use from_json. To unpack an array into one row per element, use variant_explode. It is the VARIANT counterpart of explode, and it outputs pos, key and value columns.
Checkpoint 3 of 9· Check yourself
JSON events containing a field named eventType are loaded with Auto Loader into a VARIANT column called payload. An analyst then runs SELECT payload:EventType FROM events. What does the query return?
VARIANT keeps the field names exactly as they appear in the source, and path elements are matched case-sensitively. EventType and eventType are therefore different fields. A path that matches nothing returns NULL, not an error.
“VARIANT is flexible to schema and type changes and maintains case sensitivity and NULL values present in the data source”Source: docs.databricks.com
3.Loading JSON files straight into VARIANT
Auto Loader can put each whole file record into one VARIANT column. You do this with the singleVariantColumn option. This holds up well when the source is unpredictable, because nothing about the record's shape has to match a declared schema. The trade-offs are concrete:
- A malformed record cannot be encoded as
VARIANT. - A record can be at most 16 MB. Larger records are treated like corrupt records, so in the default PERMISSIVE mode they go to the corrupt record column.
- The table schema is always the one
VARIANTcolumn, so no schema evolution happens and the rescued data column isn't supported.
(spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("singleVariantColumn", "variant_column")
.load("/Volumes/catalog_name/schema_name/volume_name/path")
.writeStream
.option("checkpointLocation", checkpoint_path)
.toTable("table_name")
)You don't have to store the whole record as VARIANT. You can declare a schema in which only some columns are VARIANT, such as name STRING, address VARIANT. You can also pass address VARIANT as a schema hint, and Auto Loader infers every other field with its normal rules. That suits a payload with a stable top level and one deeply nested branch that keeps changing.
COPY INTO uses the same option for batch loads: singleVariantColumn goes in FORMAT_OPTIONS. COPY INTO is idempotent and skips files it has already loaded (COPY INTO idempotent incremental loads). Where Auto Loader is available, Databricks recommends it over COPY INTO.
CREATE TABLE table_name (variant_column VARIANT);
COPY INTO table_name
FROM '/Volumes/catalog_name/schema_name/volume_name/path'
FILEFORMAT = JSON
FILES = ('file-name')
FORMAT_OPTIONS ('singleVariantColumn' = 'variant_column')Checkpoint 4 of 9· Fill the gap
The target table has one VARIANT column, and the source JSON changes shape often. Which Auto Loader option makes this stream write each whole record into that column?
(spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option(" ? ", "variant_column")
.load("/Volumes/catalog_name/schema_name/volume_name/path")
.writeStream
.option("checkpointLocation", checkpoint_path)
.toTable("table_name")
)singleVariantColumn tells Auto Loader to store each whole record as one VARIANT value in the named column. cloudFiles.schemaHints takes a type definition such as 'address VARIANT', not a column name, and only that field becomes VARIANT. The rescued data column isn't supported in single-VARIANT mode.
Source: docs.databricks.comSources1
4.Typed inference and nested schema hints for JSON
Often you do want JSON flattened into typed columns. Then you rely on Auto Loader's schema inference, which samples the first files it finds and records the schema at cloudFiles.schemaLocation (Auto Loader schema inference and evolution modes). JSON has one default that surprises people.
Inferring everything as a string is deliberate. A field read as INT in the sample would cause a type-mismatch failure the first time a later file held "25a" there. If you want numbers, booleans and dates typed from the sample data, as the plain Spark DataFrameReader does, set cloudFiles.inferColumnTypes to true. This option only controls how column types are inferred. It doesn't affect schema evolution, file listing or option validation.
Sometimes you need one field fixed to a type and want everything else inferred. Schema hints do this. They let you impose the types you know on an inferred schema, and they can reach inside nested data. Use a dot path for struct fields, such as user_info.dob DATE, and .element, .key or .value for arrays and maps, as in the documented hint users.element.id INT. Hints also matter for evolution. addNewColumns is rejected when you pass a full schema with .schema(...), but it works when that schema arrives as a hint.
Checkpoint 5 of 9· Check yourself
Your team ingests nested JSON with Auto Loader. They need user_info.dob read as DATE, every other field inferred, and the stream must keep evolving under addNewColumns as new fields appear. Which configuration meets all three needs?
A schema hint fixes the type of one known field, and Auto Loader still infers the others. addNewColumns also keeps working with a hint, but it is rejected when a full schema is passed with .schema(...). A withColumn cast leaves the field inferred and evolved as a string. inferColumnTypes only infers types from the sample, so it doesn't fix the field to DATE.
“addNewColumns is not allowed when the schema of the stream is provided, but does work if you provide your schema as a schema hint.”Source: docs.databricks.com
Checkpoint 6 of 9· Exam question
During Auto Loader schema inference on a directory of nested JSON files, an engineer notices that every field, including numeric and boolean values, is inferred as a string type in the target Delta table. Which configuration change causes Auto Loader to apply standard Spark type inference (numbers, booleans, dates) to the JSON fields instead of defaulting everything to strings?
Correct answer: A — Set cloudFiles.inferColumnTypes to true
- A. This option explicitly enables type detection during schema inference, so JSON fields are typed as numbers, booleans, or dates using Spark's standard inference behavior instead of defaulting every field to a string.
- B. This setting controls how newly arriving columns are handled after the schema has already been established; it does not change how the initial data types of existing fields are inferred from the JSON payloads.
- C. This setting toggles the file discovery strategy used to detect new files arriving in the source directory and has no effect on how column data types are inferred from the file contents.
- D. This setting controls whether Auto Loader validates the options passed to the stream reader and has no bearing on the data type assigned to inferred JSON columns.
Checkpoint 7 of 9· Exam question
A data engineer is building an Auto Loader stream that reads deeply nested JSON files. Downstream consumers require that a nested user_info.dob field always be materialized as a DATE column rather than whatever type Auto Loader would otherwise infer for it. Which mechanism lets the engineer force that specific nested field to a chosen type while still letting Auto Loader infer the rest of the schema automatically?
Correct answer: A — A schema hint such as "user_info.dob DATE" passed as a reader option to the stream
- A. Schema hints let you override the inferred type of a specific column path, including nested fields addressed with dot notation, while Auto Loader continues to infer the remaining schema automatically.
- B. Post-read casting on the full DataFrame changes the value's type only after inference has already happened for every field, so it does not target a single nested field during schema inference itself.
- C. A Lakeflow Spark Declarative Pipelines expectation validates or drops rows based on a condition at write time; it enforces a rule about the data rather than overriding the inferred column type during Auto Loader's schema inference step.
- D.
COPY INTOis a separate batch-loading SQL command with its ownFORCEoption for reloading files, and it does not apply to an Auto Loader streaming read or provide per-field type overrides.
Sources5
5.New JSON fields: evolution modes and the rescued data column
Sooner or later a producer adds a field. Under the default addNewColumns mode, Auto Loader merges the new column onto the end of the schema it has stored and then stops the stream with UnknownFieldException. The stream picks up the new column only after a restart, which is why Databricks recommends running these streams as Lakeflow Jobs that restart automatically. The types of existing columns never change in this mode. Which mode you choose decides whether the stream stops, and whether the new data is kept or lost.
| Mode | Does the stream stop? | Where the new field goes |
|---|---|---|
| addNewColumns (default without a provided schema) | Yes, with UnknownFieldException. A restart resumes processing | Added as a new column after the restart |
| addNewColumnsWithTypeWidening | Yes, the same as addNewColumns | Added as a column. Supported widenings such as int to long are applied, and unsupported type changes go to the rescued data column |
| rescue | No | Into the rescued data column. The schema never evolves |
| failOnNewColumns | Yes, and it won't restart until you update the schema or remove the file | Nowhere. The schema isn't updated |
| none (default with a provided schema) | No | Ignored, unless the rescuedDataColumn option is set |
rescue is the mode for a stream that must never stop and must not silently drop data. none also keeps the stream running, but without the rescued data column it discards the new fields. Wherever the rescued data column is on, it is the safety net for messy JSON. When Auto Loader infers a schema, it adds the column as _rescued_data. The column catches fields that are missing from the schema, values whose type doesn't match, and keys whose case differs from the schema. Each entry is a JSON blob of the rescued fields plus the source file path of the record. So when a reviewer finds "amount": "n/a" in a DOUBLE column, they can tell which file it came from. Provided schemas and hints are covered in Schema enforcement with provided schemas, schema hints and the rescued data column.
The rescued column also changes how parser modes treat type mismatches. With rescuedDataColumn set, a mismatch no longer drops the record in DROPMALFORMED mode or raises an error in FAILFAST mode. Only genuinely corrupt JSON fails, for example a truncated object.
Checkpoint 8 of 9· Match them up
A producer adds a new field to the JSON files an Auto Loader stream is reading. Match each cloudFiles.schemaEvolutionMode to how the stream handles that file.
Tap a term, then the definition that fits it.
rescue and none both keep the stream running, but only rescue keeps the new data by default. addNewColumns evolves the schema but needs a restart. failOnNewColumns blocks the stream until someone steps in.
“Auto Loader never evolves the schema and the stream does not fail due to schema changes. Auto Loader records all new columns”Source: docs.databricks.com
Checkpoint 9 of 9· Exam question
A team continuously lands nested JSON event files in cloud object storage and streams them with Auto Loader into a Unity Catalog-governed Delta table. They want the stream to keep running without interruption when unexpected new fields appear in the JSON payloads, while still capturing those unexpected fields for later review instead of silently dropping them. Which cloudFiles.schemaEvolutionMode setting satisfies this requirement?
Correct answer: B — rescue
- A. This is the default evolution mode, and it deliberately stops the stream with an
UnknownFieldExceptiononce new columns are detected so the schema can be updated before the stream resumes. That interruption is exactly what the team wants to avoid. - B. This mode never stops the stream for new fields; unmatched or unexpected fields are automatically written into the
_rescued_datacolumn as a JSON blob, so processing continues and the extra data is preserved for later inspection. - C. This mode halts processing permanently as soon as a new column appears and requires a manual schema update before the stream can resume, which is the opposite of uninterrupted processing.
- D. This mode ignores new columns entirely and does not preserve them unless rescued data handling is separately enabled, so the extra fields would be silently discarded rather than captured for review.
Sources5
Sources
(5)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.https://docs.databricks.com/aws/en/ingestion/variantOfficial docs
“VARIANT is a standard SQL type in Databricks Runtime 15.3 and above and is supported by tables backed by Delta Lake.”
↩︎ Storing a whole JSON record in a VARIANT column“Databricks recommends extracting frequently queried fields and storing them as non-variant columns to accelerate queries and optimize storage layout.”
↩︎ Storing a whole JSON record in a VARIANT column“The VARIANT data type cannot be used for comparisons, grouping, ordering, and set operations.”
↩︎ Storing a whole JSON record in a VARIANT column“VARIANT type can only hold records up to 16 MB in size.”
↩︎ Loading JSON files straight into VARIANT“Malformed records cannot be encoded using VARIANT type.”
↩︎ Loading JSON files straight into VARIANT“Infers the schema for other fields using standard rules.”
↩︎ Loading JSON files straight into VARIANT“Databricks recommends using Auto Loader over COPY INTO when available.”
↩︎ Loading JSON files straight into VARIANT“Because VARIANT is flexible to schema and type changes and maintains case sensitivity and NULL values present in the data source”
↩︎ Key concept“no schema evolution occurs during ingestion and rescuedDataColumn is not supported”
↩︎ Exam trap 1“VARIANT columns cannot be used for clustering keys, partitions, or Z-order keys.”
↩︎ Exam trap 2“VARIANT columns cannot be used for clustering keys, partitions, or Z-order keys.”
↩︎ Checkpoint“VARIANT is flexible to schema and type changes and maintains case sensitivity and NULL values present in the data source”
↩︎ Checkpoint“Because the entire record is recorded as a single VARIANT column, no schema evolution occurs during ingestion and rescuedDataColumn is not supported.”
↩︎ Prediction - 2.
“Use . or [<key>] to select nested fields with named keys.”
↩︎ Querying nested fields with path syntax - 3.
“All variant path elements are matched in a case-sensitive way. JSON strings are case-insensitive.”
↩︎ Querying nested fields with path syntax“The [*] syntax is not support for identifying or unpacking all elements in an array.”
↩︎ Querying nested fields with path syntax“variant_explode | explode | Outputs pos, key, and value columns.”
↩︎ Querying nested fields with path syntax - 4.
“Use the from_json function to cast nested results into more complex data types, such as arrays or structs.”
↩︎ Querying nested fields with path syntax - 5.
“To enable this behavior with Auto Loader, set the option cloudFiles.inferColumnTypes to true.”
↩︎ Typed inference and nested schema hints for JSON“You can use schema hints to enforce the schema information that you know and expect on an inferred schema.”
↩︎ Typed inference and nested schema hints for JSON“users.element.id INT”
↩︎ Typed inference and nested schema hints for JSON“addNewColumns is not allowed when the schema of the stream is provided, but does work if you provide your schema as a schema hint.”
↩︎ Typed inference and nested schema hints for JSON“Auto Loader never evolves the schema and the stream does not fail due to schema changes.”
↩︎ New JSON fields: evolution modes and the rescued data column“The data types of existing columns remain unchanged.”
↩︎ New JSON fields: evolution modes and the rescued data column“The rescued data column contains a JSON blob with the rescued columns and the source file path of the record.”
↩︎ New JSON fields: evolution modes and the rescued data column“When used together with rescuedDataColumn, data type mismatches do not cause Auto Loader to drop records in DROPMALFORMED mode”
↩︎ New JSON fields: evolution modes and the rescued data column“When Auto Loader detects a new column, the stream stops with an UnknownFieldException.”
↩︎ Exam trap 3“For formats that don't encode data types (JSON, CSV, and XML), Auto Loader infers all columns as strings (including nested fields in JSON files).”
↩︎ Prediction“Auto Loader never evolves the schema and the stream does not fail due to schema changes. Auto Loader records all new columns”
↩︎ Checkpoint