I realized that I may need to add a bit more details. Imagine that I have 2 columns in a dataframe. Both are strings, one is an ID, the other is a json string.
This can be constructed below:
>>> a1 = [{"a": 1, "b": "[{\"h\": 3, \"i\": 5} ,{\"h\": 4, \"i\": 6}]" },
... {"a": 1, "b": "[{\"h\": 6, \"i\": 10},{\"h\": 8, \"i\": 12}]"}]
>>> df1 = sqlContext.read.json(sc.parallelize(a1))
>>> df1.show()
+---+--------------------+
| a| b|
+---+--------------------+
| 1|[{"h": 3, "i": 5}...|
| 1|[{"h": 6, "i": 10...|
+---+--------------------+
>>> df1.printSchema()
root
|-- a: long (nullable = true)
|-- b: string (nullable = true)
Note that the json code is StringType. I want to write a function that creates are new column which stores the data as a nested table, like below:
root
|-- a: long (nullable = true)
|-- b: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- h: long (nullable = true)
| | |-- i: long (nullable = true)
I am using 1.6 therefore I don't have to_json cast function. I have tried to do this
>>> df1.withColumn('new', get_json_object(df1.b,'$')).show()
+---+--------------------+--------------------+
| a| b| new|
+---+--------------------+--------------------+
| 1|[{"h": 3, "i": 5}...|[{"h":3,"i":5},{"...|
| 1|[{"h": 6, "i": 10...|[{"h":6,"i":10},{...|
+---+--------------------+--------------------+
The issue is the new column created is still a string. :(