0

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
6
  • In python we use lowercase_with_underscores for variables and CapitalCase for types. Entity should be entity, etc.. That threw me for a second. Commented Apr 25, 2018 at 12:18
  • Add a print(Entity) just after for Entity in Entities: and tell us the output. Commented Apr 25, 2018 at 12:22
  • If the only thing your JSON file contains is that dictionary that would explain the error. Set 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 do for line in json_file: entity = json.loads(line) Commented Apr 25, 2018 at 12:24
  • If I have 2 rows and i am running again, i am getting an error: Commented Apr 25, 2018 at 12:55
  • File "C:/Freedom/Comparing json file.py", line 33, in <module> entities = json.load(json_file, parse_float = decimal.Decimal) File "C:\Users\meitale\AppData\Local\Programs\Python\Python36-32\lib\json_init_.py", line 299, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) File "C:\Users\meitale\AppData\Local\Programs\Python\Python36-32\lib\json_init_.py", line 367, in loads return cls(**kw).decode(s) Commented Apr 25, 2018 at 12:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

can it be connected to the python version? i am running with 3.6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.