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

524
Visualizações
Pandas DataFrame as body of FastAPI's JSONResponse

I have a Pandas DataFrame with several columns and some data that I need to return in a response body from a FastAPI application. Borrowing the DataFrame example from another similar, but different, question:

test_list = [["Joe", 34, "Accounts", 10000], ["Jack", 35, "Chemistry", 20000], ["Jill", 37, "Art", 2000]]
df = pandas.DataFrame(data=test_list, columns=["Name", "Age", "Dept.", "Salary"])

Now if I try to use dataframe.to_json as follows, the returned body is a string with every quote character escaped and is not JSON:

result = df.to_json(orient="records") #creates a json string
return JSONResponse(content = result) 

The above returns something like:

"[{\"Name:\":\"Joe\", \"Age\":\"34\"},  ...etc"

The following works (in that the returned body is valid JSON) but I don't like that the response is returned as plain text instead of JSON. Is there a better, more proper way to do this?

result = df.to_json(orient="records") 
return PlainTextResponse(content = result)
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Instead of json, convert it to dict which fastapi should be able to handle automatically:

result = df.to_dict('records')
return JSONResponse(content = result) 
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