Saving API Result Into JSON File in Python
Last Updated :
24 Apr, 2025
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 into JSON File in Python
Below is the step-by-step procedure by which we can save the API result into a JSON file in Python:
Step 1: Installation
Here, we will install the following libraries and modules before starting with the program:
- Install Python
- Install FastAPIis
- FastAPI Uvicorn
Step 2: Project Structure
Below is the project structure for this program.

Step 3: Create a Virtual Environment
In this step, we will create a virtual environment by using the following command.
python3 -m venv venv
Now, as environment is created then we have to activate the environment by using the following command.
venv\Scripts\activate
Now run a command to create and automatically write down dependencies in requirements.txt
pip freeze > requirements.txt
Step 4: Writing the FastAPI Python Code (main.py)
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 greeting message and details about the author, including name, email, and associated tags.
main.py
Python3
from fastapi import FastAPI
import json
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 5: Writing the script.py File
In this code, a GET request is made to a FastAPI endpoint ("http://localhost:8000/Hello-world") using the requests
library. If the request is successful (status code 200), the obtained JSON data is appended to an existing JSON file ("data.json") after loading its current content. If the file doesn't exist or is empty, a new list is created, and the new data is added. The final JSON content is then written back to the file, ensuring that the data retrieved from the API is persistently stored for future use.
script.py
Python3
import requests
import json
api_url = "http://localhost:8000/Hello-world"
response = requests.get(api_url)
if response.status_code == 200:
new_data = response.json()
try:
with open("data.json", "r") as json_file:
existing_data = json.load(json_file)
except (FileNotFoundError, json.decoder.JSONDecodeError):
existing_data = []
existing_data.append(new_data)
with open("data.json", "w") as json_file:
json.dump(existing_data, json_file, indent=4)
print("Data appended to data.json file.")
else:
print("Failed to retrieve data from the API. Status code:", response.status_code)
Step 6: Run the Program
We will run the following command in the terminal to run the program.
uvicorn main:app --reload
After running server you will see this in terminal:

FastAPI by default run at localhost:8000 you have to type url localhost:8000/Hello-world and enter it to see the result of api in browser. Now we have to run script.py, it will make call to the api url localhost:8000/Hello-world and once it will get data it is going to store it in the data.json file in the form of array of json objects.
Run the following command in the terminal:
python3 script.py
Output:
(data.json)

Similar Reads
Append to JSON file using Python
The full form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Pytho
2 min read
Read JSON file using Python
The full form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Pytho
4 min read
Reading and Writing JSON to a File in Python
The full form of JSON is Javascript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Pytho
3 min read
Python | Build a REST API using Flask
Prerequisite: Introduction to Rest API REST stands for REpresentational State Transfer and is an architectural style used in modern web development. It defines a set or rules/constraints for a web application to send and receive data. In this article, we will build a REST API in Python using the Fla
3 min read
Creating a JSON Response Using Django and Python
In Django, we can give responses to the front end in several ways. The simplest way to render a template or send a JSON response. JSON stands for JavaScript Object Notation and is widely used for data transfer between front-end and back-end and is supported by different languages and frameworks. In
4 min read
Reading and Writing lists to a file in Python
Reading and writing files is an important functionality in every programming language. Almost every application involves writing and reading operations to and from a file. To enable the reading and writing of files programming languages provide File I/O libraries with inbuilt methods that allow the
5 min read
Visualizing JSON Data in Python
The JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy to the read and write for humans and machines alike. The Visualizing JSON data can help in the understanding its structure and the relationships between its elements. This article will guide we through differ
5 min read
How to import JSON File in MongoDB using Python?
Prerequisites: MongoDB and Python, Working With JSON Data in Python MongoDB is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in the form of key-value pairs. JSON stands for JavaScript Object Notation
2 min read
Writing files in background in Python
How to write files in the background in Python? The idea is to use multi-threading in Python. It allows us to write files in the background while working on another operation. In this article, we will make a 'Asyncwrite.py' named file to demonstrate it. This program adds two numbers while it will al
2 min read
Convert Text file to JSON in Python
JSON (JavaScript Object Notation) is a data-interchange format that is human-readable text and is used to transmit data, especially between web applications and servers. The JSON files will be like nested dictionaries in Python. To convert a text file into JSON, there is a json module in Python. Thi
4 min read