I am trying to load a json file in my streamlit app and parse it into a pandas dataframe.
This is the json file:
{
"school_name": "local primary school",
"class": "Year 1",
"info": {
"president": "John Kasich",
"address": "ABC road, London, UK",
"contacts": {
"email": "admin@e.com",
"tel": "123456789"
}
},
"students": [
{
"id": "A001",
"name": "Tom",
"math": 60,
"physics": 66,
"chemistry": 61
},
{
"id": "A002",
"name": "James",
"math": 89,
"physics": 76,
"chemistry": 51
},
{
"id": "A003",
"name": "Jenny",
"math": 79,
"physics": 90,
"chemistry": 78
}]
}
I am using json_normalize but I get the following error βTypeError: string indices must be integersβ
def show_students(file):
json=pd.read_json(file)
df=pd.json_normalize(
json,
record_path =['students'],
meta=[
'class',
['info', 'president'],
['info', 'contacts', 'tel']
]
)
st.write(df)
uploaded_file = st.file_uploader("Choose a file", type=['json'])
if uploaded_file is not None:
show_students(uploaded_file)
Any idea how I could solve this? Thanks!