val someDF = Seq(
(8, """{"details":{"decision":"ACCEPT","source":"Rules"}"""),
(64, """{"details":{"decision":"ACCEPT","source":"Rules"}""")
).toDF("number", "word")
someDF.show(false) :
+------+---------------------------------------------------------------+
|number|word |
+------+---------------------------------------------------------------+
|8 |{"details":{"decision":"ACCEPT","source":"Rules"} |
|64 |{"details":{"decision":"ACCEPT","source":"Rules"} |
+------+---------------------------------------------------------------+
Problem statement: I want to combine all columns into 1 column with JSON types preserved inside the single output column. That is no escaping of quotes etc. like I got below.
What I tried:
someDF.toJSON.toDF.show(false)
// this escaped the quotes, which I don't want
+------------------------------------------------------------------------------------------------+
|value |
+------------------------------------------------------------------------------------------------+
|{"number":8,"word":"{\"details\":{\"decision\":\"ACCEPT\",\"source\":\"Rules\"}"} |
|{"number":64,"word":"{\"details\":{\"decision\":\"ACCEPT\",\"source\":\"Rules\"}"} |
+------------------------------------------------------------------------------------------------+
Same issue with someDF.select( to_json(struct(col("*"))).alias("value"))
What I want:
+------------------------------------------------------------------------------------------------+
|value |
+------------------------------------------------------------------------------------------------+
|{"number":8,"word":{"details":{"decision":"ACCEPT","source":"Rules"}}} |
|{"number":64,"word":{"details":{"decision":"ACCEPT","source":"Rules"}}} |
+------------------------------------------------------------------------------------------------+
Is there a way to do this?
Update: Though I used a simple dataframe here, in reality I have hundreds of columns so manually defined schema doesn't work for me.