Python Forum
TypeError: string indices must be integers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: string indices must be integers
#1
Hello everyone

I'm getting an error that makes little sense to me.
Background info: I'm running this script in AWS Lambda using the python 3.6 runtime. There's only one custom library other than the boto3 package but those aren't the issue.
The code:
import json
import paho.mqtt.client as mqtt
import boto3

def parser(event, context):
    
    msg = event['body']['best_plate']['plate']
    
    try:
        client = mqtt.Client()
        client.username_pw_set("bloof", "7NmC7xFGj3")
        client.connect("broker.secluded.io", 1883, 60)
        client.publish('lambda', str(msg), 0, False)
    except:
        print('MQTT error')
        
    try:
        dynamodb = boto3.resource('dynamodb')
        table = dynamodb.Table('alpr')
        
        table.put_item(
           Item={
                'id': event['body']['best_plate']['plate'],
                'message': str(msg),
            }
        )
    except:
        print('DynamoDB error')
       
    return {
        "statusCode": 200,
        "body": json.dumps('ohno')
    }
The purpose of this is simply to get the value of 'plate' (in the 'best_plate' field).
This is the POST request I get from the source.

{'resource': '/alprParser', 'path': '/alprParser', 'httpMethod': 'POST', 'headers': {'Accept': 'text/html, application/json', 'Accept-Charset': 'UTF-8', 'Content-type': 'application/json', 'Host': '29mc0rgpec.execute-api.ap-southeast-2.amazonaws.com', 'User-Agent': 'OpenALPR/1.0 (webhooks)', 'X-Amzn-Trace-Id': 'Root=1-5bb3fef8-d0d7ad877b61ed2d968a5a5e', 'X-Forwarded-For': '54.146.194.40', 'X-Forwarded-Port': '443', 'X-Forwarded-Proto': 'https'}, 'multiValueHeaders': {'Accept': ['text/html, application/json'], 'Accept-Charset': ['UTF-8'], 'Content-type': ['application/json'], 'Host': ['29mc0rgpec.execute-api.ap-southeast-2.amazonaws.com'], 'User-Agent': ['OpenALPR/1.0 (webhooks)'], 'X-Amzn-Trace-Id': ['Root=1-5bb3fef8-d0d7ad877b61ed2d968a5a5e'], 'X-Forwarded-For': ['54.146.194.40'], 'X-Forwarded-Port': ['443'], 'X-Forwarded-Proto': ['https']}, 'queryStringParameters': None, 'multiValueQueryStringParameters': None, 'pathParameters': None, 'stageVariables': None, 'requestContext': {'resourceId': 'crfr57', 'resourcePath': '/alprParser', 'httpMethod': 'POST', 'extendedRequestId': 'OKTG0H0tywMFytA=', 'requestTime': '02/Oct/2018:23:27:52 +0000', 'path': '/default/alprParser', 'accountId': '247020531818', 'protocol': 'HTTP/1.1', 'stage': 'default', 'requestTimeEpoch': 1538522872409, 'requestId': 'c8644d95-c69a-11e8-b8b0-276400c85b9d', 'identity': {'cognitoIdentityPoolId': None, 'accountId': None, 'cognitoIdentityId': None, 'caller': None, 'sourceIp': '54.146.194.40', 'accessKey': None, 'cognitoAuthenticationType': None, 'cognitoAuthenticationProvider': None, 'userArn': None, 'userAgent': 'OpenALPR/1.0 (webhooks)', 'user': None}, 'apiId': '29mc0rgpec'}, 'body': '{"epoch_start": 1538522864100, "camera_id": 167096507, "frame_start": 9623907, "agent_uid": "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P", "best_confidence": 94.99427032470703, "company_id": "bddecc71-5b8c-46ba-aabc-62c9efc8ecee", "version": 2, "agent_type": "alprd", "best_plate": {"plate": "817YGI", "confidence": 94.99427032470703, "region_confidence": 99, "vehicle_region": {"y": 293, "x": 372, "height": 414, "width": 414}, "region": "au-qld", "plate_index": 0, "processing_time_ms": 244.74142456054688, "candidates": [{"matches_template": 1, "plate": "817YGI", "confidence": 94.99427032470703}], "coordinates": [{"y": 566, "x": 537}, {"y": 536, "x": 614}, {"y": 570, "x": 621}, {"y": 602, "x": 545}], "matches_template": 1, "requested_topn": 10}, "vehicle": {"orientation": [{"confidence": 99.9916763305664, "name": "225"}, {"confidence": 0.005987311247736216, "name": "45"}, {"confidence": 0.0016428668750450015, "name": "135"}, {"confidence": 0.0006164500955492258, "name": "180"}, {"confidence": 5.8999859902542084e-05, "name": "315"}], "color": [{"confidence": 99.42945861816406, "name": "silver-gray"}, {"confidence": 0.2916490137577057, "name": "gold-beige"}, {"confidence": 0.16489849984645844, "name": "blue"}, {"confidence": 0.037771083414554596, "name": "white"}, {"confidence": 0.030877409502863884, "name": "brown"}], "make": [{"confidence": 97.27043151855469, "name": "toyota"}, {"confidence": 1.7690397500991821, "name": "jeep"}, {"confidence": 0.39034587144851685, "name": "daihatsu"}, {"confidence": 0.2538227438926697, "name": "suzuki"}, {"confidence": 0.08880969882011414, "name": "chery"}], "body_type": [{"confidence": 98.72554779052734, "name": "suv-crossover"}, {"confidence": 0.7218751907348633, "name": "sedan-compact"}, {"confidence": 0.4031677842140198, "name": "sedan-wagon"}, {"confidence": 0.1464931070804596, "name": "suv-standard"}, {"confidence": 0.0012662876397371292, "name": "van-mini"}], "year": [{"confidence": 65.79833984375, "name": "2010-2014"}, {"confidence": 16.67740249633789, "name": "2015-2019"}, {"confidence": 16.326322555541992, "name": "2005-2009"}, {"confidence": 1.1169471740722656, "name": "2000-2004"}, {"confidence": 0.06527841091156006, "name": "1995-1999"}], "make_model": [{"confidence": 98.77079010009766, "name": "toyota_rav-4"}, {"confidence": 0.5795552730560303, "name": "toyota_land-cruiser-prado"}, {"confidence": 0.27228206396102905, "name": "jeep_liberty"}, {"confidence": 0.06472454965114594, "name": "jeep_wrangler"}, {"confidence": 0.039640553295612335, "name": "ssangyong_korando"}]}, "best_uuid": "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522865531", "epoch_end": 1538522866715, "best_image_width": 2944, "data_type": "alpr_group", "best_image_height": 1656, "frame_end": 9623947, "is_parked": false, "web_server_config": {"agent_label": "GCTechSpace", "camera_label": "carparkside"}, "best_region": "au-qld", "uuids": ["X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522864100", "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522864511", "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522864319", "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522865167", "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522865219", "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522865351", "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522865531", "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522865274", "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522865990", "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522866276", "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522866509", "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522866168", "X2JYQMHPUF93Y0DJLEJUAYZ8MYR41BDETCDJEU8P-167096507-1538522866715"], "plate_indexes": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "travel_direction": 289.7391357421875, "country": "au", "best_plate_number": "817YGI", "best_region_confidence": 96.92308044433594, "agent_version": "2.5.103", "candidates": [{"matches_template": 1, "plate": "817YGI", "confidence": 94.99427032470703}, {"matches_template": 1, "plate": "817YGL", "confidence": 67.3626708984375}, {"matches_template": 1, "plate": "817YGT", "confidence": 67.1427230834961}]}', 'isBase64Encoded': False}

And this is the cloudWatch log I get whenever the function runs:

string indices must be integers: TypeError
Traceback (most recent call last):
File "/var/task/main.py", line 7, in parser
msg = event['body']['best_plate']['plate']
TypeError: string indices must be integers

The reason why this issue escapes me is because if I get only the event['body'], copy and paste it into the test configuration then try to do msg = event['best_plate']['plate'] and run the test, it all works flawlessly.

What am I missing?
Thanks to everyone for the help.
Reply


Messages In This Thread
TypeError: string indices must be integers - by matteoBaldini - Oct-03-2018, 12:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 11,747 Nov-02-2023, 01:20 PM
Last Post: brewer32
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,231 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,312 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,497 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,075 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,493 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,508 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,931 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,618 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  string indices must be integers when parsing Json ilknurg 3 6,441 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020