从数据帧中删除空数组字段,同时将其转换为JSON [英] remove null array field from dataframe while converting it to JSON
本文介绍了从数据帧中删除空数组字段,同时将其转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以通过不使用那些为空的字段从spark数据帧创建json:
Is there any method where i can create a json from a spark dataframe by not using those fields which are null:
假设我有一个数据框:
+-------+----------------+
| name| hit_songs|
+-------+----------------+
|beatles|[help, hey jude]|
| romeo| [eres mia]|
| juliet| null |
+-------+----------------+
我想将其转换为json,例如:
i want to convert it into a json like:
[{
name: "beatles",
hit_songs: [help, hey jude]
},
{
name: "romeo",
hit_songs: [eres mia]
},
{
name: "juliet"
}
]
如果它的值为空,我不希望json_object中的hit_songs字段
i dont want the field hit_songs in the json_object if its value is null
推荐答案
在这种情况下,请使用 to_json
函数.
Use to_json
function for this case.
df=spark.createDataFrame([("beatles",["help","hey juude"]),("romeo",["eres mia"]),("juliet",None)],["name","hit_songs"])
from pyspark.sql.functions import *
df.groupBy(lit(1)).\
agg(collect_list(to_json(struct('name','hit_songs'))).alias("json")).\
drop("1").\
show(10,False)
#+-------------------------------------------------------------------------------------------------------------------+
#|json |
#+-------------------------------------------------------------------------------------------------------------------+
#|[{"name":"beatles","hit_songs":["help","hey juude"]}, {"name":"romeo","hit_songs":["eres mia"]}, {"name":"juliet"}]|
#+-------------------------------------------------------------------------------------------------------------------+
#using toJSON function.
df.groupBy(lit(1)).\
agg(collect_list(struct('name','hit_songs')).alias("json")).\
drop("1").\
toJSON().\
collect()
#[u'{"json":[{"name":"beatles","hit_songs":["help","hey juude"]},{"name":"romeo","hit_songs":["eres mia"]},{"name":"juliet"}]}']
这篇关于从数据帧中删除空数组字段,同时将其转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文