Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exception: TypeError(string indices must be integers)

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading