I have a dataframe **df\_original** like below
[![enter image description here](https://i.stack.imgur.com/7gqGa.png)](https://i.stack.imgur.com/7gqGa.png)

I want to convert it into a nested json format like below

[![enter image description here](https://i.stack.imgur.com/SQLos.png)](https://i.stack.imgur.com/SQLos.png)

So far I hv done this

```
val df_original =data.groupBy($"unique_id").agg(collect_set(struct($"acct_no",$"ciskey")).as("accounts"))
val data1 = data.groupBy($"unique_id").agg(collect_set(struct($"acct_no",$"ciskey")).as("accounts"))
val resultDf = df_original.join(data1, Seq("unique_id")).dropDuplicates()

```

which generates the below json

```
{
  "unique_id": "12345678",
  "transaction_status": "posted",
  "amount": "116.26",
  "category": "Family",
  "email_id": "abcd@gmail.com",
  "acct_no": "51663",
  "ciskey": "47626220",
  "accounts": [
    {
      "acct_no": "51663",
      "ciskey": "47626220"
    },
    {
      "acct_no": "51663",
      "ciskey": "47626221"
    }, 
    {
      "acct_no": "51663",
      "ciskey": "47626222"
    }

  ]
}

Please help me to move forward

```