In Pyspark I want to save a dataframe as a json file but in the below format
Say this is my dataframe
>>> rdd1.show()
+----------+-----+
| f1| f2|
+----------+-----+
|AAAAAAAAAA|99999|
| BBBBBBBBB|99999|
| CCCCCCCCC|99999|
+----------+-----+
if I save the above dataframe as a json file it gives an output like below
>>>rdd1.coalesce(1).write.json("file:///test_directory/sample4")
{"f1":"AAAAAAAAAA","f2":"99999"}
{"f1":"BBBBBBBBB","f2":"99999"}
{"f1":"CCCCCCCCC","f2":"99999"}
But I want it like the below
[{"f1":"AAAAAAAAAA","f2":"99999"},{"f1":"BBBBBBBBB","f2":"99999"},{"f1":"CCCCCCCCC","f2":"99999"}]
I have tried option("multiLine", "true") and lineSep="," none seems to be working, these options are working only for read not write. Please suggest a solution for this problem