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": "",
    "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):
        record_path =['students'], 
            ['info', 'president'], 
            ['info', 'contacts', 'tel']

uploaded_file = st.file_uploader("Choose a file", type=['json'])
    if uploaded_file is not None:

Any idea how I could solve this? Thanks!

Hi @CDV welcome to Streamlit!

I tried it in my local system but I was greeted with:

Traceback (most recent call last):
  File "/home/jovial/", line 6, in <module>
    json = pd.read_json(jf)

raise ValueError(
ValueError: Mixing dicts with non-Series may lead to ambiguous ordering.

So Instead of pd.read_json() I used json.load() and it works.

import json

with open("x.json", "r") as jf:
    json_file = json.load(jf)
    # json = pd.read_json(jf) # errors!
    df = pd.json_normalize(json_file, meta=[
        ['info', 'president'],
        ['info', 'contacts', 'tel']

""" Output
          school_name   class                                           students  ...          info.address
0  local primary school  Year 1  [{'id': 'A001', 'name': 'Tom', 'math': 60, 'ph...  ...  ABC road, London, UK         123456789

[1 rows x 7 columns]


I’m sorry I’m not sure whey pandas throws an error, but it’s not related to Streamlit :grinning:

Happy Coding!


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!


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

1 Like