I have a column like this:
| column |
|---|
| [{"key":1,"value":"aaaaa"},{"key":2,"value":"bbbbb"},{"key":3,"value":"ccccc"}] |
| [{"key":1,"value":"abcde"},{"key":2,"value":"bcdef"}] |
| [{"key":1,"value":"edcba"},{"key":3,"value":"zxcvb"},{"key":4,"value":"qwert"}] |
I want to separate such column base on the keys, with each key having their column. I've tried something like this but it didn't work:
test_schema = ArrayType(StructType([StructField("key", IntegerType()), StructField("value", StringType())]))
teste = (hits_raw
.withColumn("keys", get_json_object("hitsCustomDimensions", "$[*].key"))
.withColumn("teste", explode(from_json("hitsCustomDimensions", test_schema)))
).display()
The output I want is something like this:
| column_1 | column_2 | column_3 | column_4 |
|---|---|---|---|
| aaaaa | bbbbb | ccccc | null |
| abcde | bcdef | null | null |
| edcba | null | zxcvb | qwert |