Help pls with TypeError: byte indices must be integers or slices, not str

I have been deep dive teaching myself python - and would appreciate some help i am struggling to problem solve.

I get: <TypeError: byte indices must be integers or slices, not str >for the following line of code:
WHookID = int(data['Alert_ID'])

The WHookID is data from a JSON Webhook. example below:

   "Alert_ID": "7960057",
   "Order_Descr": "Long Bull Engulf", 
   "Order_Action": "BUY",
   "Entry_Stop_Price": "18500",
   "Take_Profit": "18550",
   "Stop_Loss": "18400",
   "Contract_Qty": "1",
   "Ticker": "NQ",
   "Local_Symbol": "NQM4",
   "Contract_Type": "FUT",
   "Exchange": "CME",
   "Contract_Exp_Date": "20240621",
   "Currency": "USD",
   "Instrument Price": "18366"

As a bit of a noob to Python; i have researched and tried so many ideas - all of which havent helped. I have ran out of ideas how to problem solve and would be extremely grateful for some guidance from those here.

The full code for the class is below:

def webhook():
    data =

    if data:
        r.publish('tradingview', data)

        data_dict = request.json

        WHookID = int(data['Alert_ID'])     
        WHook_IdDB = (json.loads(WHookID))
        for value in WHook_IdDB[WHookID]:

        db = get_db()
        cursor = db.cursor()

        cursor.execute("SELECT ALERT_ID FROM WebHook_SigDB WHERE rowid = (SELECT MAX(rowid) FROM WebHook_SigDB)")
        #cur.execute("SELECT ALERT_ID FROM WebHook_SigDB ORDER BY rowid desc LIMIT 1")
        result = cursor.fetchone()

        print(f"Result = {result[0]}")
        if result == WHook_IdDB:
            print("Webhook Id:" + result + "is NOT unqiue - no DB entry")
            print("Webhook Id is unique:" + result + "Enter into DB")
                INSERT OR REPLACE INTO WebHook_SigDB (Alert_ID, Ticker, Order_Action, Contract_Qty, Entry_Stop_Price, Take_Profit, StopLoss) 
            VALUES (?, ?, ?, ?, ?, ?, ?)
        """, (data_dict['Alert_ID'],

        return data

    return {
        "code": "success"

The error is saying that data is bytes, but I’m guessing that you expected it to be a dict. It isn’t.

Recommendation: IIDPIO - If In Doubt, Print It Out!

Just before the line that’s crashing, print(data). If you’re unsure what data type it is, also put in print(type(data)). That should tell you something about what’s going on.

Hi Chris - thankyou for your quick input and help. I had already tried to Print the data and the Data Type, but for some reason i am not able to see the Print output for that module in the terminal - i suspect because i have ran it as a Flask application, as the Flask Run statements are the only info printed in the terminal. Other modules Print ok in the terminal. i am now trying to work out how to get the print statement to show.

Hi Karl - thankyou so much for your quick reply - this breakdown you have provided is helpful to assist me to think through and understand the code properly in how each works - as i got to the point of researching and trying and adding different fixes to the problem - as i am a noob to this (which we all are at one point) .I really appreciate your input here…i will go through it with deep thought to the application

The general debugging advice was good, I think, but I completely misread what you were trying to do - I assumed you were doing the usual web-scraping thing, rather than trying to serve web page content. That makes a considerable difference to the correct answer. Regardless I wish you well in the next round of debugging.

Interesting. In theory, a print call from inside a Flask app should display in the console. Have you confirmed that this is indeed the function that’s getting called? Try mutating the line of code (maybe data['Alert_ID_testing'] or something) just to see if the error changes.

1 Like

just a thought: are you sure you want data['Alert_ID'] and not data_dict['Alert_ID']like you’re using further on in the code?

Oh! Great call.

Hi Albert - thankyou for your reply and suggestion - I received an excellent and very helpful reply from Karl Knechtel early this morning that guided me through the thought process and the functional steps each of the code elements performed - and this guidance helped me to trace back and work out the exact answer you provided - data_dict['Alert_ID' … which coincidentally solved the problem.
So Albert - you are 100% correct and thankyou :-).
And a massive thankyou to Karl for his very insightful post - but also deleted this post very shortly after posting the message which i am unsure why. It was this post that led me to the answer. Luckily i recorded his lengthy post prior to him deleting it.
By the way Albert - I too have the surname ‘Visser’ :slight_smile: - family of Dutch blood.
thankyou to you all - i learnt a lot in this process.

Hi Chris - yes it is a strange issue which has had me scratching my head to figure out. However - once i solved the TypeError byte indice problem this morning; thanks to Karl Knetchel (and Albert Visser also came up with the exact solution)…the print statements now display in the terminal for that module. So the Type Error must have been overriding and preventing the print statements. Print statements work in all the other modules regardless of the errors - and also display the errors. Anyway … thankyou for your input and help here - i really appreciate it and see this is a really good community here.

1 Like

Hi Karl …i hope you get to see this message - it was your original post (that was subsequently deleted) guiding me through the thought process and understanding better the functional purpose of each of the code elements that guided me and led me to work out the solution myself; which was to reference data_dict['Alert_ID'] and not data[‘Alert_ID’].

I think i had got to the point of brain dead problem solving and ran out of steam - and your direction helped me break through this. Thankyou … it was much appreciated :slight_smile:

1 Like

Nice! I too have some Dutch blood, although on my mother’s side, so I don’t carry a Dutch surname. Maybe that’s why we all gravitated to Python??

This part is why I deleted… I didn’t want to hint too strongly towards following a wrong path. But if I taught you how to read a map, I’m happy I could help.