title: Convert dataframe to json in scala
body: <p>Assuming i have a wordcount example where i get a dataframe as word in one column and wordcount in another column, I want to collect the same and store it as an array of json in mongo collection.</p>
  
  <pre><code>eg for dataframe:
  |Word  |  Count |
  | abc   | 1   |
  | xyz   |  23  |
  </code></pre>
  
  <p>I should get the json like:</p>
  
  <pre><code>{words:[{word:"abc",count:1},{word:"xyz",count:23}]}
  </code></pre>
  
  <p>When i tried .toJSON on the dataframe and collected the value as list and added it to a dataframe the result which got stored in my mongo was a collection of string rather than collection of JSON.</p>
  
  <p>query used : </p>
  
  <pre><code>explodedWords1.toJSON.toDF("words").agg(collect_list("words")).toDF("words")
  
  result : "{\"words\":[{\"word\":\"abc\",\"count\":1},{\"word\":\"xyz\",\"count\":23}]}"
  </code></pre>
  
  <p>I am new to Scala. Any help will be good. (Will be helpful if external package aint used).</p>