0

I use this code to read lines from json file and insert them in DynamoDB via Lambda:

obj= s3.get_object(Bucket=b, Key=jsonFile)
recList=obj['Body'].read().split('\n')
for row in recList:
    table.put_item(Item=json.loads(row))

After all lines are inserted, I get this error:

no json object could be decoded 

I checked and found that my json file ends with an empty row. How can I correct my code by checking for each row if it is empty or not please? Or even better, ignore the last row as I am sure it is only the last line that is empty.

Thank you.

Haha
  • 973
  • 16
  • 43

1 Answers1

2

How about

obj= s3.get_object(Bucket=b, Key=jsonFile)
recList=obj['Body'].read().split('\n')
for row in recList:
  if row.strip():
    table.put_item(Item=json.loads(row))
woodz
  • 737
  • 6
  • 13
  • thank you. it worked. But can you tell me what does strip do exactly? accoring to the documentation https://docs.python.org/fr/2.7/library/string.html#string.strip it Returns a copy of the string with leading and trailing characters removed. But here we are using it as a boolean – Haha Mar 11 '20 at 16:28
  • find a good discussion with backgrounds [here](https://stackoverflow.com/questions/9573244/how-to-check-if-the-string-is-empty) – woodz Mar 11 '20 at 16:43