Reviewed by Aditya Kumar · Last reviewed 2026-08-08
Processing nested JSON data involves parsing the JSON string into a structured type, then accessing its elements. For arrays within the JSON, an explode operation is often necessary to flatten them…
Pro-Move: Schema inference + validation. Red Flag: Ad-hoc string parsing for nested JSON.
This easy-level Python/Coding question appears frequently in data engineering interviews at companies like TCS. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (spark) will help you answer variations of this question confidently.
Start by clearly defining the core concept being asked about. Interviewers want to see that you understand the fundamentals before diving into implementation details. Structure your answer with a definition, then explain the practical application with a concise example. The expert answer includes a code example that demonstrates the implementation pattern.
Processing nested JSON data involves parsing the JSON string into a structured type, then accessing its elements. For arrays within the JSON, an explode operation is often necessary to flatten them into individual rows.
* Spark: The primary function is from_json(col, schema). This parses a JSON string column into a StructType or ArrayType of structs. Providing a StructType schema (either manually defined or inferred using schema_of_json for dynamic scenarios) is crucial; it ensures type safety, handles schema evolution by returning null for missing fields instead of errors, and optimizes parsing. For arrays within JSON, explode(array_col) is used to create a new row for each element, effectively flattening the array. Individual fields are then accessed via dot notation (e.g., col('data.nested.field')) or getItem('field').
* Pandas: json_normalize() is the go-to function, designed to flatten semi-structured JSON into a flat DataFrame. It intelligently handles nested dictionaries and lists of dictionaries, creating new columns with configurable prefixes to avoid name collisions.
* Why: Explicit schemas and robust parsing functions are vital for data pipelines dealing with varying JSON structures, preventing job failures and ensuring consistent data quality. They allow for graceful handling of missing keys by returning null values, rather than throwing errors.
Here's a PySpark example demonstrating parsing and accessing nested fields:
from pyspark.sql.functions import from_json, col
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
json_schema = StructType([
StructField("id", StringType(), True),
StructField("details", StructType([
StructField("name", StringType(), True),
StructField("value", IntegerType(), True)
]), True)
])
# Assuming 'json_string_col' contains the JSON
df_parsed = df.withColumn("parsed_data", from_json(col("json_string_col"), json_schema))
df_final = df_parsed.select(
col("parsed_data.id"),
col("parsed_data.details.name").alias("detail_name"),
col("parsed_data.details.value").alias("detail_value")
)
Key Trade-offs:
* Flattening vs. Nested: While flattening (creating separate columns for all nested fields) simplifies SQL queries and is often preferred for analytical layers (e.g., in dbt models or Snowflake tables), it can lead to significant row explosion if arrays are large, increasing storage, processing costs, and Spark shuffle operations. Deeply nested structures, conversely, can be less performant to query directly in column-oriented databases.
* Production Strategy: A common pattern is to store the raw, nested JSON in a STRING or VARIANT column in a raw data layer (e.g., in Delta Lake or a data lakehouse) for schema evolution flexibility and auditability. Downstream, curated tables are then created, often flattened, for specific analytical use cases, optimizing for query performance.
* Cost: Flattening arrays with explode can dramatically increase row counts, impacting data volume, Spark partition sizes, and the efficiency of operations like joins and aggregations.
In the interview, also mention the implications of schema evolution and how different tools handle it (e.g., schema_of_json for inference, or manual schema updates), and the performance considerations of deep nesting versus flattening.
Pro-Move: Schema inference + validation. Red Flag: Ad-hoc string parsing for nested JSON.
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.