title: Writing nested JSON in spark scala
body: <p>My Spark-SQL is generating an output for a query by joining two tables which has one to many cardinality. I have to convert the data into JSON.
  This is how the output of the query will look like.</p>
  
  <pre><code>Address_id_parent | Address_id_child | Country_child | city_child
  1                 |      1           |     India     |    Delhi
  1                 |      1           |     US        |    NewYork
  1                 |      1           |     US        |    NewJersey
  </code></pre>
  
  <p>The above data has to be converted to JSON in this way.</p>
  
  <pre><code>{
      "Address": {
          "Address_id_parent": "1"
      },
      "Address-details": [{
          "Address_id_child": "1",
          "location": [{
                   "country":"India",
                   "city":"Delhi",
                  },
                  {
                   "country":"US",
                   "city":"NewYork",
                  },
                  {
                   "country":"US",
                   "city":"NewJersey",
                  }
                  ]
      }]
  }
  </code></pre>
  
  <p>How can I accomplish this?</p>