Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One way to eliminate additional space in Lambda's JSON output is to use the json.dumps() method with the separators parameter. This parameter should be set to a tuple of two values: (',',':'). This will remove all whitespace characters from the output and replace the default separators with a comma and a colon. Here's an example:

import json

def lambda_handler(event, context):
    data = {"name": "John", "age": 30, "city": "New York"}
    json_data = json.dumps(data, separators=(',', ':'))
    print(json_data)

This code will output the following JSON string:

{"name":"John","age":30,"city":"New York"}