πŸ—‚οΈ Working with JSON in Oracle Database and Oracle APEX – Real-Time API Integration Example

πŸ—‚οΈ Working with JSON in Oracle Database and Oracle APEX – Real-Time API Integration Example

πŸ”Ή About

JSON (JavaScript Object Notation) has become the standard format for modern web applications and APIs.

In modern enterprise applications, developers frequently work with:

βœ… REST APIs
βœ… Mobile applications
βœ… Third-party integrations
βœ… Dynamic front-end applications
βœ… AI & cloud services

Oracle Database and Oracle APEX provide powerful native support for JSON operations.

In this blog, we’ll learn:

βœ” What is JSON
βœ” JSON support in Oracle Database
βœ” JSON functions in SQL
βœ” Working with JSON in Oracle APEX
βœ” Real-time API integration example


πŸ› οΈ Tools and Technologies

  • Oracle Database
  • Oracle APEX
  • SQL
  • PL/SQL
  • REST APIs
  • JSON

πŸš€ What is JSON?

JSON stands for:

JavaScript Object Notation

It is a lightweight data exchange format used between applications and APIs.


πŸ“Œ Example JSON

{
   "empno": 101,
   "ename": "Ankur",
   "job": "Developer",
   "salary": 5000
}

🎯 Why JSON is Important?

JSON is widely used because:

βœ… Lightweight
βœ… Easy to read
βœ… API friendly
βœ… Language independent
βœ… Fast data exchange


πŸš€ JSON Support in Oracle Database

Oracle Database provides native JSON support using:

βœ… JSON data type
βœ… JSON_OBJECT
βœ… JSON_ARRAY
βœ… JSON_TABLE
βœ… JSON_VALUE
βœ… JSON_QUERY


πŸ“Œ Creating Table with JSON Column

CREATE TABLE employee_json
(
    emp_id      NUMBER,
    emp_data    CLOB CHECK (emp_data IS JSON)
);

πŸ“Œ Insert JSON Data

INSERT INTO employee_json
VALUES (
    1,
    '{
       "ename":"Ankur",
       "job":"Developer",
       "salary":5000
     }'
);

πŸ“Œ Fetch Complete JSON

SELECT emp_data
FROM employee_json;

πŸ“Œ Extract JSON Value

Use:

JSON_VALUE

Example:

SELECT
    JSON_VALUE(emp_data, '$.ename') AS employee_name,
    JSON_VALUE(emp_data, '$.job')   AS employee_job
FROM employee_json;

πŸ“Œ Extract JSON Object

Use:

JSON_QUERY

Example:

SELECT JSON_QUERY(emp_data, '$')
FROM employee_json;

πŸ“Œ Convert Table Data to JSON

Oracle can generate JSON directly from SQL.


πŸš€ Example Using JSON_OBJECT

SELECT JSON_OBJECT(
    'empno' VALUE empno,
    'ename' VALUE ename,
    'job'   VALUE job
) AS employee_json
FROM emp;

πŸ“Œ Generate JSON Array

SELECT JSON_ARRAYAGG(
    JSON_OBJECT(
        'empno' VALUE empno,
        'ename' VALUE ename
    )
) AS employees
FROM emp;

πŸš€ Real-Time Example: Employee Directory API in Oracle APEX

Now let’s build a practical real-time example.


🎯 Scenario

Suppose we want to:

βœ… Generate employee data as JSON
βœ… Expose it via REST API
βœ… Consume the JSON in Oracle APEX
βœ… Display dynamic employee cards


πŸ“Œ Step 1: Create REST API Query

Go to:

SQL Workshop β†’ RESTful Services

Create Module:

employee_api

πŸ“Œ SQL Query

SELECT JSON_ARRAYAGG(
    JSON_OBJECT(
        'empno' VALUE empno,
        'ename' VALUE ename,
        'job' VALUE job,
        'salary' VALUE sal
    )
) AS employee_data
FROM emp;

πŸ“Œ Sample API Response

[
  {
    "empno":7369,
    "ename":"SMITH",
    "job":"CLERK",
    "salary":800
  },
  {
    "empno":7499,
    "ename":"ALLEN",
    "job":"SALESMAN",
    "salary":1600
  }
]

πŸš€ Step 2: Consume JSON API in Oracle APEX

Create a region:

Create Page β†’ Cards

πŸ“Œ Data Source Type

Set:

REST Data Source

Use your API endpoint.


πŸ“Œ Map JSON Columns

JSON FieldCard Attribute
enameTitle
jobSubtitle
salaryBody

πŸš€ Step 3: Add Dynamic Styling

Go to:

Cards β†’ Appearance β†’ HTML Expression

πŸ“Œ Example

<div class="emp-card">

<h3>#ENAME#</h3>

<p>#JOB#</p>

{if SALARY/}

<span class="t-Badge t-Badge--success">
Salary: #SALARY#
</span>

{endif/}

</div>

πŸš€ Step 4: Add Custom CSS

Go to:

Page β†’ CSS β†’ Inline

πŸ“Œ CSS

.emp-card {
    padding: 15px;
    border-radius: 12px;
    background: #f8f9fa;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

πŸš€ Working with JSON_TABLE

JSON_TABLE converts JSON into relational rows.


πŸ“Œ Example

SELECT *
FROM JSON_TABLE(
    '[
      {"name":"Ankur","salary":5000},
      {"name":"Ravi","salary":7000}
    ]',
    '$[*]'
    COLUMNS (
        employee_name VARCHAR2(50) PATH '$.name',
        salary NUMBER PATH '$.salary'
    )
);

πŸ“Œ Output

EMPLOYEE_NAMESALARY
Ankur5000
Ravi7000

πŸš€ Working with APEX_JSON Package

Oracle APEX provides:

APEX_JSON

package for JSON processing.


πŸ“Œ Example

DECLARE
    l_json CLOB;
BEGIN

    APEX_JSON.initialize_clob_output;

    APEX_JSON.open_object;

    APEX_JSON.write('ename', 'Ankur');
    APEX_JSON.write('job', 'Developer');

    APEX_JSON.close_object;

    l_json := APEX_JSON.get_clob_output;

    DBMS_OUTPUT.put_line(l_json);

END;

πŸ“Œ Output

{
   "ename":"Ankur",
   "job":"Developer"
}

πŸš€ Real-World Use Cases

JSON is heavily used in:

βœ” REST APIs
βœ” Mobile applications
βœ” AI integrations
βœ” Cloud applications
βœ” External system integrations
βœ” Dynamic dashboards


πŸš€ Example Architecture

Oracle Database
       ↓
Generate JSON
       ↓
REST API
       ↓
Oracle APEX
       ↓
Dynamic UI Rendering

⚠️ Important Considerations

πŸ”Ή Validate JSON

Always validate JSON before storing.


πŸ”Ή Use JSON Indexing

For large JSON datasets:

βœ… Use JSON Search Index

to improve performance.


πŸ”Ή Avoid Large Nested JSON

Very complex JSON structures may impact performance.


πŸš€ Advantages of JSON in Oracle

βœ… Native support
βœ… High performance
βœ… API friendly
βœ… Flexible structure
βœ… Easy integration


🎯 Why Oracle APEX Developers Love JSON

JSON makes it easy to integrate Oracle APEX with:

βœ” External APIs
βœ” JavaScript frameworks
βœ” Mobile apps
βœ” Cloud services

without complicated transformations.


πŸŽ‰ Conclusion

JSON has become an essential part of modern application development.

With Oracle Database and Oracle APEX, developers can easily:

βœ… Store JSON
βœ… Generate JSON
βœ… Parse JSON
βœ… Consume REST APIs
βœ… Build dynamic applications

Whether you are integrating external services or building modern enterprise apps, mastering JSON in Oracle and APEX is a must-have skill.


Happy Coding! πŸš€

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *