I have Json file contains the the below row:
{
"tenant_EntityID": {
"s": "12345"
},
"enrtyDate": {
"s": "7.9.2000 14:53:45"
},
"first_name": {
"s": "Y7M9"
},
"last_name": {
"s": "NUYE"
},
"gender": {
"s": "male"
},
"birth_incorp_date": {
"s": "9.3.1999 14:49:44"
},
"email": {
"s": "[email protected]"
}
}
When I am trying to load it to DynamoDB by the below code:
import boto3
import json
import decimal
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
table = dynamodb.Table('Entities')
with open("C:/data/bigJson1.json") as json_file:
Entities = json.load(json_file, parse_float = decimal.Decimal)
for Entity in Entities:
table.put_item(
Item={
'tenant_EntityID':Entity['tenant_EntityID'] ,
'enrtyDate': Entity['enrtyDate'],
'first_name': Entity['first_name'],
'last_name': Entity['last_name'],
'gender': Entity['gender'],
'birth_incorp_date': Entity['birth_incorp_date'],
'email': Entity['email']
}
)
I am getting the error:
Traceback (most recent call last):
File "C:/Freedom/Comparing json file.py", line 39, in <module>
'tenant_EntityID':Entity['tenant_EntityID'] ,
TypeError: string indices must be integers
lowercase_with_underscoresfor variables andCapitalCasefor types.Entityshould beentity, etc.. That threw me for a second.print(Entity)just afterfor Entity in Entities:and tell us the output.Entity = json.load(...)and remove the for loop. Only do the loop if you have a list of dictionaries, i.e. there must be square brackets. If there are no square brackets but you have a JSON object on each line, then you need to dofor line in json_file: entity = json.loads(line)