In my previous tutorial, we built a Flask application that connected to an Oracle Database and displayed employee data in a browser.
Now, let's take it one step further.
Instead of rendering HTML, we'll expose the same Oracle table as a REST API that returns JSON. This is how modern applications communicate with mobile apps, web frontends, Oracle Integration Cloud (OIC), and other external systems.
By the end of this tutorial, you'll have an API that returns employee data directly from Oracle Database.
What We'll Build
We'll expose data from the XXFLASK_EMPLOYEE_INFO table through the following endpoint:
Instead of HTML, the response will be JSON.
Example:
[
{
"name": "Pranay",
"dept": "IT",
"salary": 60000,
"contact_info": "+9195555552367"
},
{
"name": "Anshuman",
"dept": "Finance",
"salary": 35000,
"contact_info": "+9195555552368"
}
]
Project Structure
We'll continue using the same project.
Since the database connection is already stored in json.config, we don't need to change any connection details inside the code.
Step 1: Import Required Libraries
Start by importing the required packages.
Why jsonify?
Unlike the previous tutorial where Flask returned HTML, we'll now use jsonify().
jsonify() converts Python dictionaries and lists into valid JSON responses while automatically setting the correct HTTP response headers.
Step 2: Read the Configuration File
Load the database connection dynamically.
This keeps the application configurable and avoids hardcoding database credentials.
Step 3: Initialize Oracle and SQLAlchemy
This establishes the Oracle database connection using SQLAlchemy.
Step 4: Map the Oracle Table
Create the ORM model.
This maps the Oracle table to a Python class.
Now every employee record becomes a Python object.
0 Comments