title: DataFrame to Json Array in Spark
body: <p>I am writing Spark Application in Java which reads the HiveTable and store the output in HDFS as Json Format. </p>
  
  <p>I read the hive table using <code>HiveContext</code> and it returns the DataFrame. Below is the code snippet.</p>
  
  <pre><code> SparkConf conf = new SparkConf().setAppName("App");
   JavaSparkContext sc = new JavaSparkContext(conf);
   HiveContext hiveContext = new org.apache.spark.sql.hive.HiveContext(sc);
  
  DataFrame data1= hiveContext.sql("select * from tableName")
  </code></pre>
  
  <p>Now I want to convert <code>DataFrame</code> to <code>JsonArray</code>. For Example, data1 data looks like below</p>
  
  <pre><code>|  A  |     B     |
  -------------------
  |  1  | test      |
  |  2  | mytest    |
  </code></pre>
  
  <p>I need an output like below</p>
  
  <pre><code>[{1:"test"},{2:"mytest"}]
  </code></pre>
  
  <p>I tried using <code>data1.schema.json()</code> and it gives me the output like below, not an Array.</p>
  
  <pre><code>{1:"test"}
  {2:"mytest"}
  </code></pre>
  
  <p>What is the right approach or function to convert the <code>DataFrame</code> to <code>jsonArray</code> without using any third Party libraries.</p>