您需要重新定义您的模式,因为您希望将输入视为
struct
类型,即
array<struct<foo:string>>
val mySchema = ArrayType(StructType(Seq(StructField("foo", StringType, true))),true)
val target = df.select(from_json(col("A"), mySchema).alias("A"))
target.printSchema
//root
// |-- A: array (nullable = true)
// | |-- element: struct (containsNull = true)
// | | |-- foo: string (nullable = true)
target.select(to_json($"A")).show
//+----------------+
//|structstojson(A)|
//+----------------+
//| [{"foo":"bar"}]|
//+----------------+