[Python] How to return separate JSON responses using FastAPI?

Discussão em 'Python' iniciado por Stack, Janeiro 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am not sure if this is part of OpenAPI standard. I am trying to develop an API server to replace an existing one, which is not open source and vendor is gone. One particular challenge I am facing is it returns multiple JSON objects without enclosing them either in a list or array. For example, it returns the following 3 JSON objects as they are, in separate lines:

    {"items": 10}
    {"order": "shelf", "amount": 100}
    {"id": 100, "date": "2022-01-01", "status": "X"}


    Not in a list format () or in array [].

    For example, the code below returns all 3 objects in an array:

    from fastapi import FastAPI

    app = FastAPI()

    @app.get("/")
    def read_root():
    data_1 = {"items": 10}
    data_2 = {"order": "shelf", "amount": 100}
    data_3 = {"id": 100, "date": "2022-01-01", "status": "X"}
    return data_1, data_2, data_3


    Can anyone help me to get this done with FastAPI?

    Continue reading...

Compartilhe esta Página