user7774424
user7774424

Reputation:

Importing JSON to DynamoDB

It is my first encounter with DynamoDB and I have been given a JSON File that looks like:

{
  "metadata":{
     "schemaVersion":"1.0",
     "importType":"LEX",
     "importFormat":"JSON"
  },
  "resource":{
     "description":"First Names",
     "name":"ASDUKfirstNames",
     "version":"1",
     "enumerationValues":[
     {
        "value":"Zeshan"
     },
     {
        "value":"Zoe"
     },
     {
        "value":"Zul"
     }
  ],
    "valueSelectionStrategy":"ORIGINAL_VALUE"
  }
}

and I want to import the data where value = FirstName in the DynamoDB Table that I have created named customerDetails that contains items CustomerID, FirstName and LastName.

Is there a way to utilize the boto3 put-item function to loop over the contents of the JSON file replacing value with FirstName?

Upvotes: 0

Views: 627

Answers (1)

michaelbahr
michaelbahr

Reputation: 4973

You should use Python to do the data transformation. You can find the boto3 DDB docs here.

import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('customerDetails')

json_data = { ... load the data into a dict here ... }

for enumeration_value in json_data['resouce']['enumerationValues']:
  ddb_item = {
    "CustomerID": 123,
    "FirstName": enumeration_value['value']]
  }
  table.put_item(Item=ddb_item)

Upvotes: 0

Related Questions