Structs, arrays, maps and JSON strings — query them in place, explode them safely and flatten them for the lake.
Semi-structured data — JSON events, API payloads, CDC messages — arrives nested. Spark models it with three complex types: a struct (a fixed set of named fields, like a row inside a row), an array (an ordered list of one element type) and a map (arbitrary keys to values of one type).
spark.read.json infers structs for JSON objects and arrays for JSON lists — never maps; ask for a MAP<STRING, STRING> in an explicit schema when the keys are dynamic. Inferred fields come out sorted by name. Each line must be one JSON document (JSON Lines) unless you set multiLine to true.
orders = spark.read.json("s3://landing/orders/") # JSON Lines
orders.printSchema()
# root
# |-- customer: struct (nullable = true)
# | |-- address: struct (nullable = true)
# | | |-- city: string (nullable = true)
# | | |-- zip: string (nullable = true)
# | |-- id: string (nullable = true)
# |-- items: array (nullable = true)
# | |-- element: struct (containsNull = true)
# | | |-- qty: long (nullable = true)
# | | |-- sku: string (nullable = true)
# | | |-- unit_price: double (nullable = true)
# |-- order_id: string (nullable = true)Reach the last step for the run summary: the key takeaways from this lesson.