I have a column that contains an array of strings,
I need to convert it into a JSON list of string?
original value: [a,b,c]
transformed value: "[\"a\",\"b\",\"c\"]"
You can combine struct, to_json and get_json_object:
import org.apache.spark.sql.functions._
val jsonArray = get_json_object(
to_json(struct($"YOUR_COLUMN".as("data"))),
"$.data"
)
df.select(jsonArray)
Something like below could also help variableDF = variableDF.withColumn(f.name, to_json(struct(col(f.name))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With