Home > Software engineering >  Exception: TypeError(string indices must be integers)
Exception: TypeError(string indices must be integers)

Time:02-16

I have written the below python function (a snippet of the full code) to work in AWS Lambda. The purpose of it is to take a GeoJSON from an S3 bucket and parse it accordingly.

Once parsed, it is placed back into JSON format (data) and then should be inserted into the specified database using

        
                    bulk_item['uuid'] = str(uuid.uuid4())
                    bulk_item['name'] = feature_name
                    bulk_item['type'] = feature_type
                    bulk_item['info'] = obj
                    bulk_item['created'] = epoch_time
                    bulk_item['scope'] = 2

                    data = json.dumps(bulk_item)
                    print(data)
                    
           
                    self.database.upsert_record(self.organisation, json_doc=data)  

                
        except Exception as e:
            print(f'Exception: {e.__class__.__name__}({e})')

The db_access file in which the above is relating to is another python script. The function upsert_record is as below:

def upsert_record(self, organisation,
        json_doc={}):

My code is working perfectly until I try to upsert it into the database. Once this line is gotten to, it throws the error

Traceback (most recent call last):
File "/var/task/s3_asset_handler.py", line 187, in process_incoming_file
self.database.upsert_record(self.organisation, json_doc=data)
File "/opt/python/database_access.py", line 1218, in upsert_record
new_uuid = json_doc['uuid']
TypeError: string indices must be integers

I can't seem to figure out the issue at all

CodePudding user response:

You are trying to get an element from a JSON object, but passing a string.

The

data = json.dumps(bulk_item)

creates a string representing the object.

Try using bulk_item on it's own.

  • Related