title: Write a dataframe into a JSON array file with Spark
body: <p>We have the following code:</p>
  
  <pre><code> val sparkSession = SparkSession.builder
    .master("local")
    .appName("example")
    .getOrCreate()
  
  val data = Seq(
    Row(1, "a","b","c","d"),
    Row(5, "z","b","c","d")
  
  )
  
  val schema = StructType(
    List(
      StructField("id", IntegerType, true),
      StructField("f2", StringType, true),
      StructField("f3", StringType, true),
      StructField("f4", StringType, true),
      StructField("f5", StringType, true)
    )
  )
  
  val df1 = sparkSession.createDataFrame(
    sparkSession.sparkContext.parallelize(data),
    schema
  )
  </code></pre>
  
  <p>Goal is write that dataframe in a JSON array :</p>
  
  <pre><code>[{"id":1,"f2":"a","f3":"b","f4":"c","f5":"d"},
   {"id":5,"f2":"z","f3":"b","f4":"c","f5":"d"}]
  </code></pre>
  
  <p>So we need this two square brackets but dataframe size is over 50Gb so the solution <strong><em>df1.toJSON.collect.mkString("[", "," , "]" )</em></strong> is not valid. Are there some way to solve this issue with Spark with a good performance?</p>
  
  <p>Thanks in advance</p>