I have an complex/nested JSON, that i need to transform into DataFrame (Python). I could get the first part, but i'm struggling to solve the second part.
import requests
from pandas.io.json import json_normalize
import json
url = 'url'
headers = {'api-key':'key'}
resp = requests.get(url, headers = headers)
print(resp.status_code)
r = resp.content
r
responses = json.loads(r.decode('utf-8'))
responses
Output (responses)
{'count': 39,
'requestAt': '2020-06-09T20:10:23.201+00:00',
'data': {'Id1': {'id': 'Id1',
'groupId': '1',
'label': 'Question 1',
'options': {'1_1': {'id': '1_1',
'prefix': 'A',
'label': 'Alternative A',
'isCorrect': True},
'1_2': {'id': '1_2',
'prefix': 'B',
'label': 'Alternative B',
'isCorrect': False},
'1_3': {'id': '1_3',
'prefix': 'C',
'label': 'Alternative C',
'isCorrect': False}}}}}
df = DataFrame(responses['data'])
df.T
Output (DataFrame.T):
+-----+---------+------------+-------------+
| id | groupId | label | options |
+-----+---------+------------+-------------+
| Id1 | 1 | Question 1 | **JSON 2** |
+-----+---------+------------+-------------+
**JSON 2** (all inside the cell above)
{'1_1': {'id': '1_1',
'prefix': 'A',
'label': 'Alternative A',
'isCorrect': True},
'1_2': {'id': '1_2',
'prefix': 'B',
'label': 'Alternative B',
'isCorrect': False},
'1_3': {'id': '1_3',
'prefix': 'C',
'label': 'Alternative C',
'isCorrect': False}}
I need to open the JSON 2 into DataFrame too.
Desired output:
+-----+---------+------------+--------+---------------+-----------+
| id | groupId | label | prefix | label | isCorrect |
+-----+---------+------------+--------+---------------+-----------+
| Id1 | 1 | Question 1 | A | Alternative A | True |
| Id1 | 1 | Question 1 | B | Alternative B | False |
| Id1 | 1 | Question 1 | C | Alternative C | False |
+-----+---------+------------+--------+---------------+-----------+
How do i get the desired output? Thanks.