Reputation: 13
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
Upvotes: 0
Views: 502
Reputation: 37193
When you read the JSON string into Entities the result is a dict, with keys "tenant_EntityID"
and so on. The statement for Entity in Entities
iterates over that dict, giving you the keys of the dict, which are strings.
It looks as though you need something more like this:
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:
Entity = json.load(json_file, parse_float = decimal.Decimal)
table.put_item(
Item={
'tenant_EntityID':Entity['tenant_EntityID'] ,
'enrtyDate': Entity['enrtyDate']['s'],
'first_name': Entity['first_name']['s'],
'last_name': Entity['last_name']['s'],
'gender': Entity['gender']['s'],
'birth_incorp_date': Entity['birth_incorp_date']['s'],
'email': Entity['email']
}
)
It's just my own guess that you want the values associated with the 's'
keys.
You say that it fails "when you run with 2 rows." Most likely the JSON should really be cast as a list of dictionaries, not a single dictionary.
Upvotes: 1