services web amazon - Comment convertir un article de dynamo boto3 en un dictionnaire régulier en Python

Mots clés : pythondictionaryboto3python

meilleur 2 Réponses services web amazon - Comment convertir un article de dynamo boto3 en un dictionnaire régulier en Python

vote vote

100

import boto3  low_level_data = {   "ACTIVE": {     "BOOL": True   },   "CRC": {     "N": "-1600155180"   },   "ID": {     "S": "bewfv43843b"   },   "params": {     "M": {       "customer": {         "S": "TEST"       },       "index": {         "N": "1"       }     }   },   "THIS_STATUS": {     "N": "10"   },   "TYPE": {     "N": "22"   } }  # Lazy-eval the dynamodb attribute (boto3 is dynamic!) boto3.resource('dynamodb')  # To go from low-level format to python deserializer = boto3.dynamodb.types.TypeDeserializer() python_data = {k: deserializer.deserialize(v) for k,v in low_level_data.items()}  # To go from python to low-level format serializer = boto3.dynamodb.types.TypeSerializer() low_level_copy = {k: serializer.serialize(v) for k,v in python_data.items()}  assert low_level_data == low_level_copy 
vote vote

82

from boto3.dynamodb.types import TypeDeserializer deserializer = TypeDeserializer()  document = { "ACTIVE": { "BOOL": True }, "CRC": { "N": "-1600155180" }, "ID": { "S": "bewfv43843b" }, "params": { "M": { "customer": { "S": "TEST" }, "index": { "N": "1" } } }, "THIS_STATUS": { "N": "10" }, "TYPE": { "N": "22" } } deserialized_document = {k: deserializer.deserialize(v) for k, v in document.items()} print(deserialized_document) 

Questions similaires