return value of python AWS Lambda.

0

I am trying to read dynamodb table and return values in python of Lambda. My return function is below

        return {
            'statusCode': 200,
            # 'headers': {
            #     'Access-Control-Allow-Headers': 'Content-Type',
            #     'Access-Control-Allow-Origin': '*',
            #     'Access-Control-Allow-Methods': 'OPTIONS,GET'
            #     },
            'body': json.dumps(table.scan()['Items'][0])
        }

The problem is json.dumps(). Normally, I use table.scan()['Items'] as the value, and it applied 3 tables. Two tables work well but one table doesn't. If I use table.scan()['Items'][0], it works. However, I would like to whole table values.

gefragt vor 2 Jahren903 Aufrufe
3 Antworten
2

Decimal object is not JSON serializable. Use a package that can do that for you [1] or consider casting the Decimal into a float using a helper function [2].

  1. Use simplejson instead of json as it will do the decimal conversion for you. This would mean you need to use a Lambda layer to import that pip package.

    import simplejson as json

  2. Use a custom class to do the conversion meaning you can avoid the Lambda layer:

from decimal import Decimal

class DecimalEncoder(json.JSONEncoder):
  def default(self, obj):
    if isinstance(obj, Decimal):
      return str(obj)
    return json.JSONEncoder.default(self, obj)
json.dumps(item, cls=DecimalEncoder)
profile pictureAWS
EXPERTE
beantwortet vor 2 Jahren
0

When you say "one table doesn't work" could you provide more information? Such as an error message? Or what you're expecting.

As I understand it, table.scan() only scans a single table so I'm not sure what is meant by "it applied 3 tables".

Running json.dumps(table.scan()['Items']) will return all of the items in the table. Running json.dumps(table.scan()['Items'][0]) will return the first element of the list.

profile pictureAWS
EXPERTE
beantwortet vor 2 Jahren
0

My code is

def lambda_handler(event, context): dynamodb = boto3.resource("dynamodb") table = dynamodb.Table("OrderList") items = table.scan()['Items'] return { 'statusCode': 200, 'body': json.dumps(items[1]) }

Response is { "statusCode": 200, "body": "{"TotalPrice": "101", "PartName": "Test Part", "ClientID": "user001", "UnitPrice": "50", "OrderDate": "28-10-2022", "PartID": "P003", "Status": "Sent", "Quantity": "1", "OrderID": "O0001"}" }

################################### return { 'statusCode': 200, 'body': json.dumps(items[1]) }

Response is { "errorMessage": "Object of type Decimal is not JSON serializable", "errorType": "TypeError", "requestId": "5849b770-4dc6-4858-82ed-65fbecab1f6a", "stackTrace": [ " File "/var/task/lambda_function.py", line 76, in lambda_handler\n 'body': json.dumps(items)\n", " File "/var/lang/lib/python3.9/json/init.py", line 231, in dumps\n return _default_encoder.encode(obj)\n", " File "/var/lang/lib/python3.9/json/encoder.py", line 199, in encode\n chunks = self.iterencode(o, _one_shot=True)\n", " File "/var/lang/lib/python3.9/json/encoder.py", line 257, in iterencode\n return _iterencode(o, 0)\n", " File "/var/lang/lib/python3.9/json/encoder.py", line 179, in default\n raise TypeError(f'Object of type {o.class.name} '\n" ] }

beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen