Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

517
Visualizações
AWS Lambda returning JSON data as string

I am using AWS Lambda to create my APIs and want to return an array's data in JSON format. However, when I call the lambda, it is able to return the required JSON data but it is coming as a string in double quotes. I tried running the same code in my Python IDE and everything works fine but when I try to return it in Lambda it is coming as a string. Has this got something to do with how Lambda handles the return statement in Python functions? This is how I'm returning data in my Lambda:

return {
            'statusCode': 200,
            'headers': {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": 'GET, POST, PUT, DELETE, OPTIONS'
            },
            'body': json.dumps(json_data,default = myconverter)
        }

Here json_data is a python list which is populated with the data that is being retrieved from the database for the specific unique ID passed by the user and myconverter is the JSON encoder that I've written.

The output that I'm getting is:

{
  "statusCode": 200,
  "headers": {
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS"
  },
  "body": "[{\"Dp_Record_Id\": 2, \"DP_TYPE\": \"NSDL\", \"DP_ID\": \"40877589\", \"CLIENT_ID\": \"1232\", \"Default_flag\": \"Y\"}]"
}

Here, I want the "body" to just return an array of the data without double quotes, like this:

"body": [{\"Dp_Record_Id\": 2, \"DP_TYPE\": \"NSDL\", \"DP_ID\": \"40877589\", \"CLIENT_ID\": \"1232\", \"Default_flag\": \"Y\"}]

Please let me know if this is possible and how can it be done. Any help on this would be appreciated

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I'm not sure if this is what you want, but you can just do 'body': json_data. I tested this now in my λ function:

Lambda function

import json

def lambda_handler(event, context):

    json_data = [{"Dp_Record_Id": 2, 
                  "DP_TYPE": "NSDL",
                  "DP_ID": "40877589", 
                  "CLIENT_ID": "1232", 
                  "Default_flag": "Y"}]

    return {'statusCode': 200,
            'headers': {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": 'GET, POST, PUT, DELETE, OPTIONS'
            },
            'body': json_data
        }    

Result

Response:
{
  "statusCode": 200,
  "headers": {
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS"
  },
  "body": [
    {
      "Dp_Record_Id": 2,
      "DP_TYPE": "NSDL",
      "DP_ID": "40877589",
      "CLIENT_ID": "1232",
      "Default_flag": "Y"
    }
  ]
}
over 4 years ago · Santiago Trujillo Relatório

0

Maybe the client is not understanding the type correctly, try adding the following into the "headers" dictionary:

"content-type":"application/json",
over 4 years ago · Santiago Trujillo Relatório

0

I ran into the same issue while I was creating an API by doing things from my memory. Posting this answer for my future self and others.

The issue was that whatever I return in the lambda is getting passed as response to the API. Instead only the data inside body should be passed as response.

This was happening because when I created the resource I forgot to check the checkbox to Use Lambda Proxy Integration. Here is what you have to do:

  1. Delete your existing method and create a new one. This time make sure you check Use Lambda Proxy Integration.API Gateway with Lambda proxy
  2. Create a deployment to your stage. Until this is done, the change will not reflect. This is where I got stuck and spend most of my time.
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda