Return Data in JSON Format Using FastAPI in Python
Last Updated :
28 Apr, 2025
FastAPI is a modern, fast, web framework for building APIs with Python 3.7+ based on standard Python type hints. It is designed to be easy to use and efficient, providing automatic generation of OpenAPI and JSON Schema documentation. In this article, we will see how to return data in JSON format using FastAPI in Python.
How to Return Data in Json Format Using Fastapi in Python
Below is the step-by-step procedure by which we can return data in JSON format using FastAPI in Python:
Step 1: Installation
Here, we will install the following libraries and modules before starting.
- Install Python
- Install FastAPI
- Install uvicorn
Step 2: Create a Virtual Environment
In this step, we will create the virtual environment in Python. To create a virtual environment, write the following command in your terminal.
python3 -m venv venv
Now, activate the virtual environment by using the following command:
venv\Scripts\activate
Now run a command to create and automatically write down dependency in requirements.txt
pip freeze > requirements.txt
Step 3. Write the FastAPI Python Code
In this code, a FastAPI application is created with a single endpoint ("/Hello-world"). When a GET request is made to this endpoint, the Hello
function is executed, returning a JSON response containing a nested structure with a "message," "details," and information about the author (name and email), along with a list of tags. This demonstrates how FastAPI effortlessly handles JSON serialization, allowing developers to return complex data structures as API responses.
main.py
Python3
from fastapi import FastAPI
app = FastAPI()
@app.get("/Hello-world")
def Hello():
data = {
"message": "hello world",
"details": {
"author": {
"name": "Tushar",
"email": "[email protected]"
},
"tags": ["fastapi", "python", "web"]
}
}
return data
Step 4: Run the Program
Now run the file using command in terminal.
uvicorn main:app --reload
After that you will something like image below in terminal

Now copy the following command and paste it to the browser to run the following program:
http://127.0.0.1:8000/Hello-world
This is basically says that your server is running at localhost:8000 ,this is the default port at which fastapi server running locally. But it will take you to the home route which we have not defined so it will show this thing.

It is just showing the default error handling you can go to your route and see that it is working. just enter this in tab localhost:8000/Hello-world.
Output:
JSON Data Returned Using FastAPIVideo Demonstration
Similar Reads
Saving API Result Into JSON File in Python As Python continues to be a prominent language for web development and data processing, working with APIs and storing the obtained data in a structured format like JSON is a common requirement. In this article, we will see how we can save the API result into a JSON file in Python. Saving API Result
3 min read
Convert Bytes To Json using Python When dealing with complex byte data in Python, converting it to JSON format is a common task. In this article, we will explore different approaches, each demonstrating how to handle complex byte input and showcasing the resulting JSON output. Convert Bytes To JSON in PythonBelow are some of the ways
2 min read
How to Extract or Parse JSON from a String in Python Here, we are given a string and we have to parse JSON format from the string in Python using different approaches. In this article, we will see how we can parse JSON from a string in Python. Example: Input: json_string = '{"India": "Delhi", "Russia": "Moscow", "Japan": "Tokyo"}' Output: {'India': 'D
3 min read
Convert CSV to JSON using Python Converting CSV to JSON using Python involves reading the CSV file, converting each row into a dictionary and then saving the data as a JSON file. For example, a CSV file containing data like names, ages and cities can be easily transformed into a structured JSON array, where each record is represent
2 min read
Extract Multiple JSON Objects from one File using Python Python is extremely useful for working with JSON( JavaScript Object Notation) data, which is a most used format for storing and exchanging information. However, it can become challenging when dealing with multiple JSON objects stored within a single file. In this article, we will see some techniques
3 min read