How to normalize a json file when using streamlit file_uploader?

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!

Hey @CDV,

Again, welcome to the streamlit community! :star2: :partying_face: :tada:

if this solution worked for you please let us know and mark it as the solution for @joe733 and anyone who visits your question in the future! :rainbow:

Happy Streamlit-ing!
Marisa

1 Like

This works perfectly, thank you very much for this solution!