Использование to_json()
является правильным подходом, но содержимое должно быть передано как struct
в зависимости от ситуации:
val df = Seq((0,"Joe",100,"st-1"), (1,"Tom",200,"st-2")).toDF("id","name","payable","strategy")
val result = df.select(
to_json(struct(
struct($"name",
array(struct($"strategy",$"payable")) as "payments"
) as "payload")
) as "jsonValue"
)
result.show(false)
+-------------------------------------------------------------------------+
|jsonValue |
+-------------------------------------------------------------------------+
|{"payload":{"name":"Joe","payments":[{"strategy":"st-1","payable":100}]}}|
|{"payload":{"name":"Tom","payments":[{"strategy":"st-2","payable":200}]}}|
+-------------------------------------------------------------------------+