The following JSON contains a Nested attribute with the name “result”, which contains an array of Key-Value pairs.
{
"result": [
[
{
"key": "projects.name",
"value": "Project 1",
"type": "TEXT"
},
{
"key": "projects.status",
"value": "Archived",
"type": "ENUM"
},
{
"key": "user_tasks.start_date",
"value": "2021-07-08 11:59:34",
"type": "DATETIME"
},
{
"key": "user_tasks.name",
"value": "Section 1",
"type": "TEXT"
},
{
"key": "track_user.duration",
"value": "00:40:02",
"type": "INT"
},
{
"key": "project_sections.question_count",
"value": "24",
"type": "SMALLINT"
},
{
"key": "project_sections.assigned_to_users",
"value": "[email protected]",
"type": "JSON"
}
],
[
{
"key": "projects.name",
"value": "Project 2",
"type": "TEXT"
},
{
"key": "projects.status",
"value": "Archived",
"type": "ENUM"
},
{
"key": "user_tasks.start_date",
"value": "2021-07-08 11:59:34",
"type": "DATETIME"
},
{
"key": "user_tasks.name",
"value": "Section 2",
"type": "TEXT"
},
{
"key": "track_user.duration",
"value": "00:40:02",
"type": "INT"
},
{
"key": "project_sections.question_count",
"value": "23",
"type": "SMALLINT"
},
{
"key": "project_sections.assigned_to_users",
"value": "[email protected]",
"type": "JSON"
}
],
[
{
"key": "projects.name",
"value": "Project 3",
"type": "TEXT"
},
{
"key": "projects.status",
"value": "Archived",
"type": "ENUM"
},
{
"key": "user_tasks.start_date",
"value": "2021-07-20 21:30:00",
"type": "DATETIME"
},
{
"key": "user_tasks.name",
"value": "Internal Due Date",
"type": "TEXT"
},
{
"key": "track_user.duration",
"value": "21:22:49",
"type": "INT"
},
{
"key": "project_sections.question_count",
"value": "0",
"type": "SMALLINT"
},
{
"key": "project_sections.assigned_to_users",
"value": "[email protected]",
"type": "JSON"
}
]
}
Now, what I want is to expand this JSON, and have all the Keys in the Nested array section, like in the “Expected Output” section below using Spark SQL / Scala:
I tried using explode and pivot functions but it's not working properly.

