How to Use JSON_TABLE in Oracle APEX

How to Use JSON_TABLE in Oracle APEX

How to Use JSON_TABLE in Oracle APEX with a Real-Life Example

Dealing with JSON data is a common task when building modern applications, especially when you're interacting with APIs. In Oracle APEX, you can take advantage of Oracle Database's powerful JSON handling capabilities to make this process smooth and efficient. One such tool is the JSON_TABLE function, which lets you work with JSON data as if it were a regular relational table. In this blog, we’ll break it down step by step and show you a practical example.


What Is JSON_TABLE?

JSON_TABLE is a SQL function that helps you transform JSON data into a tabular format. This makes it easy to query and manipulate JSON attributes using SQL.

With JSON_TABLE, you can:

  • Parse JSON stored in your database.

  • Extract specific fields from JSON objects or arrays.

  • Use the extracted data in APEX components like Interactive Grids, Reports, and Charts.


Why Use JSON_TABLE in Oracle APEX?

If you work with APIs or JSON files, JSON_TABLE can save you a lot of time. It helps you:

  • Display JSON data in a user-friendly format.

  • Perform complex queries on JSON without having to write complicated code.

  • Seamlessly integrate API data into your APEX applications.


Example: Employee Directory from an External API

Let’s say you’re building an Employee Directory module in Oracle APEX. An external API provides employee data in JSON format, and you need to display this data in an Interactive Grid. Here’s how you can do it:


Step-by-Step Guide

1. Get the JSON Data

First, you need to fetch the JSON data. You can do this using Oracle’s UTL_HTTP package or by importing it manually into a table. For simplicity, let’s assume the JSON data is stored in a table called EMPLOYEE_JSON in a column named JSON_DATA.

Here’s what the JSON might look like:

{
  "employees": [
    {"id": 1, "name": "Alice", "department": "HR", "salary": 60000},
    {"id": 2, "name": "Bob", "department": "Finance", "salary": 75000},
    {"id": 3, "name": "Charlie", "department": "IT", "salary": 90000}
  ]
}

2. Use JSON_TABLE to Extract Data

Here’s the SQL query to turn that JSON into a table:

SELECT 
    jt.id,
    jt.name,
    jt.department,
    jt.salary
FROM 
    EMPLOYEE_JSON ej,
    JSON_TABLE(
        ej.json_data, 
        '$.employees[*]' 
        COLUMNS (
            id          NUMBER PATH '$.id',
            name        VARCHAR2(50) PATH '$.name',
            department  VARCHAR2(50) PATH '$.department',
            salary      NUMBER PATH '$.salary'
        )
    ) jt;

What’s Happening Here?

  • ej.json_data: Refers to the JSON column in the EMPLOYEE_JSON table.

  • $.employees[*]: Points to the JSON array of employees.

  • The COLUMNS clause maps JSON attributes (like id, name, etc.) to table columns with their respective data types.

3. Display Data in an Interactive Grid

  • Go to your Oracle APEX app and create a new Interactive Grid.

  • Use the above SQL query as the source for the grid.

  • Customize the grid columns for better readability (e.g., set the salary column to display as currency).


Tips and Tricks

Validate Your JSON Data

Before using JSON_TABLE, ensure your JSON data is valid:

SELECT json_data 
FROM EMPLOYEE_JSON
WHERE json_data IS JSON;

Handle Nested JSON

If your JSON has nested objects, you can use nested COLUMNS or join multiple JSON_TABLE functions.

Automate the Process

Use a PL/SQL process to fetch JSON data from an API and insert it into your table automatically. You can schedule this as a job for regular updates.

Thanks for the reading till the last hope it helps!!!!!