You have to use json.loads not json.dumps.

Try this:

import json

event = {
   "Records":[
      {
         "messageId":"916f5e95-b2f6-4148-9c62-2ac8e764f06c",
         "receiptHandle":"AQEBmLuoGWtLtFFgvyCFdSPMJh2HKgHOIPWNUq22EOwCzGT8iILZm97CE6j4J6oR71ZpDr3sgxQcJyVZ+dmmvGl+fFftT9GCJqZYrjMGsR2Q6WsMd8ciI8bTtDXyvsk8ektd7UGfh4gxIZoFp7WUKVRcMEeBkubKd8T4/Io81D0l/AK7MxcEfCj40vWEsex1kkGmMRlBtdSeGyy7fJgUq5CFAYWciiWtbSit8S0Y38xZPmsIFhoxP0egQRoJcW4aUgMi469Gj5+khizetybtgC8vux5NCg/IejxcCueXkQ7LKVF8kfRdqRSUYB6DsOrGgfmZpK4wpXIarByNz0R2p7J88meYpj2IVULv/emXsSYaKG4rXnpbH4J9ijbLWckYLAd7wPDzCYri1ZSTgAz0kchsEw==",
         "body":"{\n\"name\": \"aniket\",\n\"tag\": \"hello\"\n}",
         "attributes":{
            "ApproximateReceiveCount":"1",
            "SentTimestamp":"1602046897707",
            "SenderId":"AIDAR3BXDV4FCWXL56NUU",
            "ApproximateFirstReceiveTimestamp":"1602046897712"
         },
         "messageAttributes":{

         },
         "md5OfBody":"98da683a47692b39c1d43bd4fa21ed89",
         "eventSource":"aws:sqs",
         "eventSourceARN":"arn:aws:sqs:ap-south-1:126817120010:documentation",
         "awsRegion":"ap-south-1"
      }
   ]
}

parsed = json.loads(event['Records'][0]['body'])
print(json.dumps(parsed, indent=4, sort_keys=True))

Output:

{
    "name": "aniket",
    "tag": "hello"
}
Answer from baduker on Stack Overflow
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with python › define lambda function handler in python
Define Lambda function handler in Python - AWS Lambda
January 31, 2026 - The following example Python Lambda function code takes in information about an order, produces a text file receipt, and puts this file in an Amazon S3 bucket: import json import os import logging import boto3 # Initialize the S3 client outside of the handler s3_client = boto3.client('s3') # Initialize the logger logger = logging.getLogger() logger.setLevel("INFO") def upload_receipt_to_s3(bucket_name, key, receipt_content): """Helper function to upload receipt to S3""" try: s3_client.put_object( Bucket=bucket_name, Key=key, Body=receipt_content ) except Exception as e: logger.error(f"Failed t
🌐
Quora
quora.com › How-do-I-parse-JSON-data-from-AWS-Lambda-functions-in-Python
How to parse JSON data from AWS Lambda functions in Python - Quora
If I were to pass the S3 object ... · Author has 727 answers and 2.5M answer views · 9y · Stop! There is no way to serialize a lambda function into JSON....
Discussions

How to retrieve well formatted JSON from AWS Lambda using Python - Stack Overflow
I have a function in AWS Lambda that connects to the Twitter API and returns the tweets which match a specific search query I provided via the event. A simplified version of the function is below. ... More on stackoverflow.com
🌐 stackoverflow.com
AWS Python Lambda Integration to Return JSON response directly - Serverless Framework - Serverless Forums
Hi, I’m trying to find out how can I return the response as a JSON directly. This is a function on AWS with Lambda Proxy Integration. All default setting. The goal is to from the python lambda function, the HTTP response client gets is directly a JSON object, instead of a string serialization ... More on forum.serverless.com
🌐 forum.serverless.com
0
November 19, 2019
Access json request from python in python AWS lambda function - Stack Overflow
I have an API in the AWS API gateway, connected to an AWS Lambda function in python, using lambda proxy integration. On my local computer, I am testing this API in python. I want to send a dictiona... More on stackoverflow.com
🌐 stackoverflow.com
Malformed JSON in AWS Lambda using Python.
Probably Postman believes that the output is plain text. Try to set "Content-Type" header to "application/json". More on reddit.com
🌐 r/learnpython
2
1
August 1, 2023
🌐
Paepper
paepper.com › blog › posts › parsing-json-data-from-aws-lambda-functions-in-python
Parsing JSON data from AWS lambda functions in Python :: Read. Hack. Learn. Repeat. — This blog features state of the art applications in machine learning with a lot of PyTorch samples and deep learning code. You will learn about neural network optimization and potential insights for artificial intelligence for example in the medical domain.
March 31, 2020 - Let’s have a very pointless lambda function which takes the name of a user and returns it with some additional data back as JSON: def handler(event, context): user = event.get('user_name', None) if user is None: raise Exception('Please provide a user.') return { 'hello': 'world', 'user': user } Alright, easy enough. And here is how you would call it, pass the name and get the response back: import boto3 def get_from_lambda(): lambda_client = boto3.client('lambda') lambda_dict = {'user': 'hans_peter'} result = lambda_client.invoke( FunctionName='testfunction', InvocationType='RequestResponse', LogType='Tail', Payload=json.dumps(lambda_dict).encode('utf-8') ) return json.loads(result['Payload'].read().decode())
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with python
Building Lambda functions with Python - AWS Lambda
These features must be enabled ... in Lambda, you can deploy your own Python runtime build with these features enabled, using a container image or custom runtime. In Python 3.12 and later Python runtimes, functions return Unicode characters as part of their JSON ...
🌐
Serverless Forums
forum.serverless.com › serverless framework
AWS Python Lambda Integration to Return JSON response directly - Serverless Framework - Serverless Forums
November 19, 2019 - This is a function on AWS with Lambda Proxy Integration. All default setting. The goal is to from the python lambda function, the HTTP response client gets is directly a JSON object, instead of a string serialization ...
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › malformed json in aws lambda using python.
r/learnpython on Reddit: Malformed JSON in AWS Lambda using Python.
August 1, 2023 -

Hello LeanPython Sub-reddit!

I come to you because I have a problem formatting JSON in AWS Lambda.

I would like to tell you a little more, what happens is that I am developing an API in AWS Lambda, I created the database in PostgreSQL, and I am using Python to perform the queries, what happens is that I had a datatimes management error, because these values could not be formatted in a JSON, so I decided to make an encoder in a class which is responsible for carrying out this process of formatting it to JSON, but what happens is that it generates a JSON which is very poorly readable, in addition to being a headache when trying to manipulate it in the Frontend, below I will show you what I did and I would like to consult with you about a possible solution to this problem, please.

Below you will find the encoder class I was talking about:

class DateTimeEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, datetime):
            return o.isoformat()

        return json.JSONEncoder.default(self, o)

In the following function I am using json.dumps to format the output of the database query into JSON, in conjunction with the encoder:

def lambda_handler(event, context):
    cur = conn.cursor(cursor_factory=RealDictCursor)
    cur.execute("SELECT * FROM users")
    results = cur.fetchall()
    return  json.dumps(results, cls=DateTimeEncoder)

When deploying this in AWS in a Lambda, and when making the HTTP request through Postman, it generates the following poorly formatted output:

"[{\"id_usuario\": 2, \"cc\": \"123456789\", \"telefono\": \"987654321\", \"nombre\": \"John\", \"apellido\": \"Doe\", \"fecha_registro\": \"2023-07-03T02:34:38.452359\"}, {\"id_usuario\": 3, \"cc\": \"1013655876\", \"telefono\": \"2085686\", \"nombre\": \"Jane\", \"apellido\": \"Doe\", \"fecha_registro\": \"2023-07-03T20:08:42.603593\"}]"

What I want is for the above JSON to look like this:

[
  {
    "id_usuario": 2,
    "cc": "123456789",
    "telefono": "987654321",
    "nombre": "John",
    "apellido": "Doe",
    "fecha_registro": "2023-07-03T02:34:38.452359"
  },
  {
    "id_usuario": 3,
    "cc": "1013655876",
    "telefono": "2085686",
    "nombre": "Jane",
    "apellido": "Doe",
    "fecha_registro": "2023-07-03T20:08:42.603593"
  }
]

If you ask me, I have made other queries that have not had to use the json.dumps and it has generated the JSON with the correct format, however, when I implement the json.dumps in conjunction with the encoder, it generates that output which is difficult to read, I sincerely hope you can help me because I don't know what else to do.

🌐
DEV Community
dev.to › jenad88 › day-41-using-python-in-an-aws-lambda-function-3ckf
Day 41: Using Python in an AWS Lambda Function - DEV Community
May 27, 2023 - 1) First, create the 4 .json files mentioned above somewhere in a local folder. 2) In the AWS Console, navigate to AWS Lambda and Create a Function with a name of your choice. Make sure to select Python 3.10, 3.9 or 3.8 as the Runtime.
Top answer
1 of 2
6

So there are 3 problems:

Problem 1: In your example event, ['Records'][0]['Sns']['Message'] is a str in JSON format. That means that you need to parse to a dict like this:

 message = event['Records'][0]['Sns']['Message']
 message = json.loads(message)

Problem 2: message['Trigger']['Dimensions'] is a list but you are trying to access it like if it were a dict. So you only need to change your code to:

message = message['Trigger']['Dimensions'][0]['name']

Problem 3: Message is a str that means that you need to verify that is a plain str or json str (otherwise you are going to have problems with multiple structures and types). For that your code could look like:

 message = event['Records'][0]['Sns']['Message']
    if isinstance(message, str):
        try:
            message = json.loads(message)
        except Exception as e:
            print(e) # Or do nothing, this is just to log the error
    elif isinstance(message, list):
        message = message[0]
    # Maybe evaluate bool, tuple, etc other types

    print('RESPONSE', message['Trigger']['Dimensions'][0]['name'] if isinstance(message, dict) else message)

However I would also recommend to make it more extensible iterating the elements that you know are list. And for safety reasons (trying to avoid null pointer exceptions), use the get() function with a default value. http://www.tutorialspoint.com/python/dictionary_get.htm . Try maybe to create a function to parse structures and make it reusable.

Good luck!

2 of 2
1

Just as Records is a list, so you use ['Records'][0]['Sns']..., so is Dimensions, so again you need to access the first element.

Top answer
1 of 4
15

When you invoke the lambda locally or through the Lambda console, you are invoking that lambda directly and so your lambda receives exactly what you're sending.

When you invoke it through API Gateway, API Gateway creates the event object for you based on your HTTP request. It adds the HTTP headers, path, query strings, payload, etc.

Here's a summary of what you're getting as an event from an API Gateway invocation:

{
    "resource": "Resource path",
    "path": "Path parameter",
    "httpMethod": "Incoming request's method name"
    "headers": {Incoming request headers}
    "queryStringParameters": {query string parameters }
    "pathParameters":  {path parameters}
    "stageVariables": {Applicable stage variables}
    "requestContext": {Request context, including authorizer-returned key-value pairs}
    "body": "A JSON string of the request payload."
    "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
}

Reference: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-simple-proxy-for-lambda-input-format

As you can see, the body will be sent to you as a string which you can parse using json.loads().

2 of 4
10
  • The request is fully packaged and sent as a single string in the 'body' key of the event dict.
  • This behaviour is different from the test console or invoking from CLI which has only the payload in the event dict meaning event.get('type') works directly.

Example code showing how to access the value of "type" key in payload:

import json

def lambda_handler(event, context):
    body_str = event.get("body", "{}")
    body_str = body_str if body_str else "{}"
    body_obj = json.loads(body_str)
    a = body_obj.get("type", "")
Top answer
1 of 3
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) ```
2 of 3
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" ] }
🌐
Medium
vinayakpandey-7997.medium.com › capturing-lambda-logs-in-json-format-d384f7c028c0
Capturing Lambda Logs In JSON Format - Vinayak Pandey - Medium
November 21, 2023 - Recently AWS announced native support for capturing Lambda logs in JSON format. So let’s see how that works. Step 1: Create a Python 3.10 Lambda function with following code:
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › create your first lambda function
Create your first Lambda function - AWS Documentation
To learn more about the Lambda ... in Python. ... The function lambda_handler takes two arguments, event and context. An event in Lambda is a JSON formatted document that contains data for your function to process. If your function is invoked by another AWS service, the ...
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with python › log and monitor python lambda functions
Log and monitor Python Lambda functions - AWS Lambda
For AWS Lambda to filter your application logs according to their log level, your function must use JSON formatted logs. You can achieve this in two ways: Create log outputs using the standard Python logging library and configure your function to use JSON log formatting.
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › monitoring, debugging, and troubleshooting lambda functions › working with lambda function logs › configuring json and plain text log formats
Configuring JSON and plain text log formats - AWS Lambda
Configuring the format of the system logs Lambda sends to CloudWatch doesn’t affect Lambda Telemetry API behavior. When you configure your function's log format as JSON, application log outputs written using supported logging libraries and methods are captured as a JSON object that contains key value pairs with the following keys. "timestamp" - the time the log message was generated ... "requestId" (Python, .NET, and Node.js) or "AWSrequestId" (Java) - the unique request ID for the function invocation
🌐
AWS re:Post
repost.aws › questions › QUY2t9fdTCRxOgeYlhZMkAPQ › how-do-i-use-json-lambda-output-in-step-functions
How do I use JSON Lambda output in Step Functions | AWS re:Post
June 23, 2020 - Also, you do not want to json.dump the object that you return. Just return a map and that will be included in your output. ... def lambda_handler(event, context): # TODO implement body = { 'Test': 'abc', 'Value': 123 } return body ... "Hello": { "Type": "Task", "Resource": "arn:aws:lambda:eu-west-1:xxxxxxxxx:function:HelloWorld:$LATEST", "ResultPath": "$.LambdaResult", "Next": "World" },
🌐
ActiveState
activestate.com › home › blog › how to create a serverless rest api with python and aws lambda
How to use Python and AWS Lambda to build a Serverless REST API
April 22, 2021 - An overwhelming benefit of these ... ... Create a handler function where we can provide the code. The handler accepts JSON formatted input and returns the same....