Converting Spark JSON Columns to Struct

Apache Spark is an open-source distributed computing system that provides an easy-to-use and robust framework for handling big data processing. One common task in big data analysis is dealing with JSON (JavaScript Object Notation) formatted data. JSON is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Spark, JSON data can be processed using various methods, but one common requirement is to convert JSON columns into structures (structs) that can be more easily manipulated and analyzed within the Spark DataFrame API.

Understanding JSON and Structs in Spark

Before diving into the process of converting JSON columns to structs, it is important to understand what these terms mean in the context of Spark.

JSON is a format that encodes data as key-value pairs, making it easy to structure a wide range of data types. In Spark, a column with a JSON string can be seen as containing a mini-structured document in each row.

A StructType, on the other hand, is a complex data type within Spark SQL that represents a structured row itself. It is a collection of StructFields that defines the column name, column data type, and a boolean flag to specify if the field can accept null values or not.

Reading JSON Data into Spark

The first step in working with JSON in Spark is to load the JSON data. Let’s start by reading a simple JSON dataset into a Spark DataFrame.


import org.apache.spark.sql.{SparkSession, DataFrame}

val spark: SparkSession = SparkSession.builder()
  .appName("JSON to Struct")
  .master("local")
  .getOrCreate()

// Sample JSON data
val jsonData: String = """[
  {"name": "John Doe", "age": 32, "address": {"city": "New York", "state": "NY"}},
  {"name": "Jane Smith", "age": 25, "address": {"city": "Los Angeles", "state": "CA"}}
]"""

// Create a DataFrame from JSON data
val jsonDF: DataFrame = spark.read.json(spark.sparkContext.parallelize(Seq(jsonData)))

jsonDF.printSchema()
jsonDF.show()

Assuming that the provided JSON data is correctly formatted and valid, the `printSchema()` method will display the inferred schema as follows:

root
 |-- name: string (nullable = true)
 |-- age: long (nullable = true)
 |-- address: struct (nullable = true)
 |    |-- city: string (nullable = true)
 |    |-- state: string (nullable = true)

And the `show()` method will output:

+--------+---+----------------------+
| name   |age|address               |
+--------+---+----------------------+
|John Doe| 32|{New York, NY}        |
|Jane Doe| 25|{Los Angeles, CA}     |
+--------+---+----------------------+

Converting JSON Strings to Structs

It is common to have a DataFrame with a column that contains JSON strings. To better manipulate this data within Spark SQL, it is helpful to convert this JSON string into a StructType. Let’s consider the following DataFrame with a JSON string column:


import org.apache.spark.sql.functions.from_json
import org.apache.spark.sql.types._

// Define the schema representing the JSON structure
val schema: StructType = new StructType()
  .add("name", StringType)
  .add("age", IntegerType)
  .add("address", new StructType()
    .add("city", StringType)
    .add("state", StringType))

// Assume we have a DataFrame with a column "jsonString" containing JSON data
val jsonDataFrame: DataFrame = spark.createDataFrame(Seq(
  ("{\"name\": \"John Doe\", \"age\": 32, \"address\": {\"city\": \"New York\", \"state\": \"NY\"}}")
)).toDF("jsonString")

// Use the `from_json` function to convert the JSON string to a StructType
val jsonToStructDF: DataFrame = jsonDataFrame.withColumn("parsedJson", from_json(col("jsonString"), schema))

jsonToStructDF.printSchema()
jsonToStructDF.show(false)

The new schema, after parsing the JSON string to a struct, will be as follows:

root
 |-- jsonString: string (nullable = true)
 |-- parsedJson: struct (nullable = true)
 |    |-- name: string (nullable = true)
 |    |-- age: integer (nullable = true)
 |    |-- address: struct (nullable = true)
 |    |    |-- city: string (nullable = true)
 |    |    |-- state: string (nullable = true)

And the `show(false)` method will output:


+---------------------------------------------------------------------------------+------------------------------+
|jsonString                                                                       |parsedJson                    |
+---------------------------------------------------------------------------------+------------------------------+
|{"name": "John Doe", "age": 32, "address": {"city": "New York", "state": "NY"}}  |{John Doe, 32, {New York, NY}}|
+-----------------------------------------------------------------+----------------------------------------------+

Exploding Nested Structs

After converting a JSON string to a StructType, you might want to flatten the structure to simplify further data manipulation. This often involves “exploding” nested structs into separate columns.

// Flattening the "address" struct from the "parsedJson" column
val flattenedDF: DataFrame = jsonToStructDF.select(
  col("parsedJson.name").as("name"),
  col("parsedJson.age").as("age"),
  col("parsedJson.address.city").as("city"),
  col("parsedJson.address.state").as("state")
)

flattenedDF.printSchema()
flattenedDF.show()

The schema for the flattened DataFrame will be:

root
 |-- name: string (nullable = true)
 |-- age: integer (nullable = true)
 |-- city: string (nullable = true)
 |-- state: string (nullable = true)

And the `show()` method will output:

+--------+---+---------+-----+
|    name|age|     city|state|
+--------+---+---------+-----+
|John Doe| 32| New York|   NY|
+--------+---+---------+-----+

Handling Complex Nested JSON

In real-world scenarios, you may encounter JSON with more complex nested structures. Converting such JSON requires a schema definition that reflects this complexity. You would typically create a StructType that includes other StructType or ArrayType objects, depending on the JSON structure.


// Define a more complex schema with nested structures and arrays
val complexSchema: StructType = new StructType()
  .add("name", StringType)
  .add("age", IntegerType)
  .add("addresses", ArrayType(new StructType()
    .add("city", StringType)
    .add("state", StringType)
  ))

// Sample JSON data with an array of addresses
val complexJsonData: String = """{"name": "John Doe", "age": 32, "addresses": [{"city": "New York", "state": "NY"}, {"city": "San Francisco", "state": "CA"}]}"""

// Ingesting the complex JSON data with the defined schema
val complexJsonDF: DataFrame = spark.read.schema(complexSchema).json(spark.sparkContext.parallelize(Seq(complexJsonData)))

complexJsonDF.printSchema()
complexJsonDF.show(false)

The schema and output will be:

root
 |-- name: string (nullable = true)
 |-- age: integer (nullable = true)
 |-- addresses: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- city: string (nullable = true)
 |    |    |-- state: string (nullable = true)
+--------+---+-------------------------------------+
|name    |age|addresses                            |
+--------+---+-------------------------------------+
|John Doe|32 |[{New York, NY}, {San Francisco, CA}]|
+--------+---+-------------------------------------+

Conclusion

In this article, we’ve covered the intricacies of working with JSON data in Apache Spark, how to convert JSON strings within DataFrame columns to StructType objects, and how to handle nested structures and arrays. We’ve examined creating schemas that accurately describe JSON data, parsing complex nested JSON, and flattening structures for easier data manipulation and analysis. Mastering these techniques will enhance your ability to work efficiently with JSON data in Spark. As you work with different kinds of JSON data, remember to adjust your schemas accordingly to ensure you are accurately representing your data within Spark’s powerful DataFrame API.

About Editorial Team

Our Editorial Team is made up of tech enthusiasts deeply skilled in Apache Spark, PySpark, and Machine Learning, alongside proficiency in Pandas, R, Hive, PostgreSQL, Snowflake, and Databricks. They're not just experts; they're passionate educators, dedicated to demystifying complex data concepts through engaging and easy-to-understand tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top