Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Spark How can I convert a column array[string] to a string with JSON array in it?

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\"]"

like image 600
sagioto Avatar asked May 21 '26 08:05

sagioto


2 Answers

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)
like image 116
user10138913 Avatar answered May 23 '26 08:05

user10138913


Something like below could also help variableDF = variableDF.withColumn(f.name, to_json(struct(col(f.name))))

like image 28
peeyush Avatar answered May 23 '26 08:05

peeyush