I have a Spark DataFrame with below columns
uuid|some_data
"A" |"ABC"
"B" |"DEF"
I need to convert this into a nested JSON of below format,
{"data":[{"attributes":[{"uuid":"A","some_data":"ABC"}]}]}
{"data":[{"attributes":[{"uuid":"B","some_data":"DEF"}]}]}
I tried the below code to achieve this,
val jsonDF= dataFrame.select(
to_json(struct(dataFrame.columns.map(column):_*)).alias("attributes")
)
val jsonDF2= jsonDF.select(
to_json(struct(jsonDF(column):_*)).alias("data")
)
val jsonDF3= jsonDF2(
to_json(struct(jsonDF2.columns.map(column):_*)).alias("value")
).selectExpr("CAST(value as STRING)")
Ended up getting the below format,
{"data": {"attributes": {"uuid":"A","some_data":"ABC}}}
{"data": {"attributes": {"uuid":"B","some_data":"DEF}}}
Please let me know what changes I need to make to get it to the required format.