我正在尝试将dataframe转换为嵌套的json。
基本上,最终输出是在现场进行的。” id
“与其他字段的嵌套json格式保持一致。
“使用字段的json格式” rank
“作为密钥和” desc
“和” percent
“作为价值观。谢谢你的帮助!
来源data:-
val df = Seq(
("1", "ABC", "1","91.68"),
("1", "BCD", "2","89.03"),
("1", "DEF", "3","78.32"),
("1", "XYZ", "4","70.64")
).toDF("id", "desc", "rank", "percent")
+---+----+----+-------+
|id |desc|rank|percent|
+---+----+----+-------+
|1 |ABC |1 |91.68 |
|1 |BCD |2 |89.03 |
|1 |DEF |3 |78.32 |
|1 |XYZ |4 |70.64 |
+---+----+----+-------+
预期产量:
+---+--------------------------------------------------------------------------------------------------------------------------------------------------------+
|id |json |
+---+--------------------------------------------------------------------------------------------------------------------------------------------------------+
|1 |{"1":{"desc":"ABC","percent":"91.68"},"2":{"desc":"BCD","percent":"89.03"},"3":{"desc":"DEF","percent":"78.32"},"4":{"desc":"XYZ","percent":"70.64"}} |
+---+--------------------------------------------------------------------------------------------------------------------------------------------------------+
1条答案
按热度按时间chhkpiq41#
您可以使用一个简单的自定义项来根据需要修复json:
然后用
withColumn
方法使用以前的自定义项和to_json
方法,该方法从给定列创建jsonstring:输出