Home > Software engineering >  python: clean dynamoDB AWS item?
python: clean dynamoDB AWS item?

Time:01-21

With AWS DynamoDB calls, we sometime get complex of item which precise the type of every element in the item. It can be useful but it's a mess to extract the data.

{
 "a": {
  "S": "AAAA"
 },
 "myList": {
  "L": [
   {
    "S": "T1"
   },
   {
    "S": "T2"
   },
   {
    "S": "TH"
   }
  ]
 },
 "checkList": {
  "L": [
   {
    "M": {
     "id": {
      "S": "telesurveyor"
     },
     "check": {
      "BOOL": true
     }
    }
   }
  ]
 },
 "createdAt": {
  "N": "1672842152365"
 },
}

We need to transform it to this:

{
 "a": "AAAA",
 "myList": ["T1","T2","TH"],
 "checkList": [
   {
     "id": "telesurveyor",
     "check": true
   }
  ],
 "createdAt": 1672842152365,
}

Is there an AWS boto3 way to do it ? If yes what is it ? If no, how to do it manually ?

CodePudding user response:

Use the high-level Table interface instead of the low-level Client interface. The former auto-marshals values between native DynamoDB attributes (e.g. "count" : { "N": "23" }) and native Python (e.g. "count": 23)

For example:

import boto3

TABLE = "my-data"

dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(TABLE)

item = table.get_item(Key={"pk": "1", "sk": "2"})

if 'Item' in item:
    print(item["Item"])

CodePudding user response:

You can achieve this in two ways:

1. Resource Table Client

This is a higher level client which abstracts DynamoDB JSON and allows you to use native JSON objects for both writing and reading to/from DynamoDB.

Client instantiation is similar to before except you state the Table resource and provide it with a table name.

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

2. TypeSerializer

The second option is useful if you have already used the low level client but want to unmarshall data for specific purposes. Its uses boto3 TypeSerializer:

import boto3
boto3.resource('dynamodb')
deserializer = boto3.dynamodb.types.TypeDeserializer()

item = {
    "a": {
        "S": "AAAA"
    },
    "myList": {
        "L": [
            {
                "S": "T1"
            },
            {
                "S": "T2"
            },
            {
                "S": "TH"
            }
        ]
    },
    "checkList": {
        "L": [
            {
                "M": {
                    "id": {
                        "S": "telesurveyor"
                    },
                    "check": {
                        "BOOL": "True"
                    }
                }
            }
        ]
    },
    "createdAt": {
        "N": "1672842152365"
    },
}

data = {k: deserializer.deserialize(v) for k, v in item.items()}
print(data)

  • Related