I am new to python and trying to extract the data from JSON object to table format using Dataframe
Sample structure of JSON
{
"subject": "Computer",
"records": [
{
"name": "Section A",
"elements": [
{
"type": "easy",
"name": "Section A Module 1",
"elements": [
{
"type": "text",
"name": "SectionQ1",
"title": "Some question?",
"readOnly": true
},
{
"type": "text",
"name": "SectionQ2",
"title": "some other question?",
"isRequired": true
}
]
}
],
"title": "Section A details"
},
{
"name": "Section B",
"elements": [
{
"type": "medium",
"name": "Section B Module 1",
"elements": [
{
"type": "radiogroup",
"name": "SectionQ1",
"title": "some question?",
"choices": [
{
"value": "1",
"text": "Choose 1"
},
{
"value": "2",
"text": "Choose 2"
},
{
"value": "3",
"text": "Choose 3"
}
],
"colCount": 3
},
{
"type": "radiogroup",
"name": "SectionQ2",
"title": "some other question?",
"description": "question 2",
"isRequired": true,
"choices": [
{
"value": "value 1",
"text": "choice 1"
},
{
"value": "value 2",
"text": "choice 2"
},
{
"value": "value 3",
"text": "choice 3"
}
]
}
],
"title": "title"
}
],
"title": "Title"
}
]
}
I'm trying to get inner elements array as table linking to outer elements.
so table could be inner_elements, outer_elements , records.
I have tried to below code along with few other approaches.
df = pd.read_json (r'sample.json')
df.columns = df.columns.map(lambda x: x.split(".")[-1])
print(df)
The expected output will be like
dataframe records
Name Title
Section A Section A details
Section B Section B details
dataframe elements1
Key(records) Type Name
Section A easy Section A Module 1
Section B medium Section B Module 1
dataframe elements2
Key(elements1) type name title readOnly Description isRequired colCount
Section A Module 1 text SectionQ1 Some question? true
Section A Module 1 text SectionQ2 some other question? true
Section B Module 1 radiogroup SectionQ1 some question? 3
Section B Module 1 radiogroup SectionQ2 some other question? question 2 true
dataframe choice
Key(elements2) type name value text
Section B Module 1 radiogroup SectionQ1 1 Choose 1
Section B Module 1 radiogroup SectionQ1 2 Choose 2
Section B Module 1 radiogroup SectionQ1 3 Choose 3
Section B Module 1 radiogroup SectionQ2 value 1 choice 1
Section B Module 1 radiogroup SectionQ2 value 2 choice 2
Section B Module 1 radiogroup SectionQ2 value 3 choice 3
However not getting any clue to could I proceed further. Please guide me to achieve the same