Search This Blog

Tuesday, July 1, 2025

Processing JSON from Table

 Processing JSON from a table in Oracle APEX is a powerful technique that allows developers to handle complex data structures stored as JSON within database columns. JSON (JavaScript Object Notation) is widely used for exchanging data between applications due to its lightweight and flexible format. In APEX, working with JSON data stored in tables enables you to parse, extract, and manipulate nested or hierarchical information efficiently. This capability is especially useful when integrating with RESTful services or handling modern data formats without extensive relational redesign. Understanding how to process JSON directly from tables can streamline your application’s data handling and improve performance.

Processing JSON from a table in Oracle APEX involves extracting and manipulating JSON data stored within database columns. Modern applications often store data in JSON format to capture complex, nested information flexibly. Oracle Database provides native JSON support, which you can leverage in APEX to efficiently query and work with JSON data directly from your tables.

First, ensure your table has a column defined to store JSON data, typically as a CLOB or VARCHAR2 datatype with JSON content. Oracle supports validation of JSON data types to guarantee data integrity. To extract values from JSON stored in a table column, use Oracle’s built-in JSON functions such as JSON_VALUE, JSON_QUERY, and JSON_TABLE. These functions let you parse JSON strings and return scalar values or nested objects.

For example, assume you have a table named EMP_DATA with a column EMP_INFO storing employee details in JSON format. You can write a SQL query to extract employee name and department like this:

SELECT
  JSON_VALUE(emp_info, '$.name') AS employee_name,
  JSON_VALUE(emp_info, '$.department') AS department
FROM emp_data;

In APEX, use such SQL queries as your source for Classic Reports, Interactive Reports, or other regions to display JSON-derived data dynamically. Additionally, JSON_TABLE is a powerful function when you want to transform JSON arrays into relational rows and columns, making it easier to join or filter data.

To update or manipulate JSON data, you can use functions like JSON_MERGEPATCH or PL/SQL with JSON_OBJECT and JSON_ARRAY methods. This allows you to modify JSON content stored in your tables programmatically.

Remember to index your JSON columns using Oracle’s JSON search indexes for performance improvements when querying large JSON datasets.

By processing JSON directly in Oracle APEX from table columns, you can seamlessly integrate flexible, schema-less data with your application logic, enhancing both capability and efficiency without needing external parsing tools.

To display JSON data stored in the "GEOMETRY" column of your USCG_DATA table in Oracle APEX, you'll need to parse the JSON stored in this CLOB column and extract the relevant data for display in a readable format.

Assuming the JSON stored in the "GEOMETRY" column is a well-structured JSON object, you can use SQL queries and the Oracle SQL functions like JSON_VALUE, JSON_QUERY, or APEX_JSON to extract values from the JSON structure.

To display the JSON data from the "GEOMETRY" column in your USCG_DATA table in Oracle APEX, you can:

  • Use SQL functions like JSON_VALUE or JSON_QUERY to extract values from the JSON.

  • Display the extracted values in APEX components such as Interactive Reports, Forms, or Interactive Grids.

  • Use PL/SQL to perform more complex processing if necessary.

Below is a step-by-step guide on how to display and extract JSON data from the "GEOMETRY" column in your APEX application.


Understanding the JSON Structure in "GEOMETRY"

Before you proceed, inspect the JSON structure in the "GEOMETRY" column. You need to understand the structure to know what specific data you want to extract. For example, if the "GEOMETRY" column stores something like:

{

  "type": "Point",

  "coordinates": [ -118.291, 34.056 ]

}

You can extract the "type" and "coordinates" values.


How Do I Use SQL to Extract JSON Data

You can write SQL queries in APEX to extract and display the JSON data from the CLOB column ("GEOMETRY") using Oracle’s JSON functions.


Here’s an example of how to query the JSON data stored in the "GEOMETRY" column to display specific values.

Query to extract the "type" and "coordinates" from the "GEOMETRY" JSON column:


SELECT

    ID,

    JSON_VALUE(GEOMETRY, '$.type') AS geometry_type,

    JSON_VALUE(GEOMETRY, '$.coordinates[0]') AS longitude,

    JSON_VALUE(GEOMETRY, '$.coordinates[1]') AS latitude

FROM USCG_DATA;

In this example:

  • JSON_VALUE(GEOMETRY, '$.type'): Extracts the "type" field from the JSON.

  • JSON_VALUE(GEOMETRY, '$.coordinates[0]'): Extracts the longitude from the "coordinates" array (first element).

  • JSON_VALUE(GEOMETRY, '$.coordinates[1]'): Extracts the latitude from the "coordinates" array (second element).


HOW DO I Use the SQL Query in APEX

You can use the above SQL query to display the extracted data in an APEX report, Interactive Grid, or other components.

Example: Displaying the Data in an Interactive Report (IR)

  1. Create an Interactive Report (IR)

    • Go to App Builder in your Oracle APEX application.

    • Create a new Interactive Report or Interactive Grid.

    • In the SQL Query section of the report, use the query mentioned above:

SELECT

    ID,

    JSON_VALUE(GEOMETRY, '$.type') AS geometry_type,

    JSON_VALUE(GEOMETRY, '$.coordinates[0]') AS longitude,

    JSON_VALUE(GEOMETRY, '$.coordinates[1]') AS latitude

FROM USCG_DATA;

  • Run the report, and it will display the "ID", "geometry_type", "longitude", and "latitude" in the report.

Example: Displaying JSON Data in a Form

You can also display the parsed JSON data in form fields if needed. Here's how:

  1. Create a Form:

    • Create a new Form in APEX based on the USCG_DATA table.

  2. Use a Query to Extract Data:

    • Use a SQL query similar to the one above to extract the JSON values in the Form source. For example:

SELECT

    ID,

    JSON_VALUE(GEOMETRY, '$.type') AS geometry_type,

    JSON_VALUE(GEOMETRY, '$.coordinates[0]') AS longitude,

    JSON_VALUE(GEOMETRY, '$.coordinates[1]') AS latitude

FROM USCG_DATA

WHERE ID = :P1_ID; -- Assuming :P1_ID is the primary key item for the form

  1. Map the Results to Form Fields:

    • Map the extracted JSON values (e.g., geometry_type, longitude, latitude) to the appropriate form items (e.g., P1_GEOMETRY_TYPE, P1_LONGITUDE, P1_LATITUDE).

Handling Complex JSON Structures

If the "GEOMETRY" column contains more complex nested JSON data, you can use the JSON_QUERY function or JSON_TABLE to extract arrays or nested objects.


Example: Using JSON_QUERY for Nested JSON

If "GEOMETRY" contains an array of coordinates and you want to extract all of them, you could use JSON_QUERY:

SELECT

    ID,

    JSON_QUERY(GEOMETRY, '$.coordinates') AS coordinates

FROM USCG_DATA;

This query will return the entire coordinates array as a JSON object. You can then manipulate it further in APEX or process it with PL/SQL.


In conclusion, processing JSON stored in tables within Oracle APEX unlocks new possibilities for managing dynamic and semi-structured data. By leveraging Oracle’s native JSON functions and APEX features, developers can efficiently query, transform, and display JSON data without needing complex parsing routines outside the database. This approach enhances application flexibility and simplifies integration with external APIs or modern data sources. Mastering JSON processing from tables is a valuable skill for building responsive, data-rich Oracle APEX applications that meet today’s evolving data demands.

How Do I Use RESTful Web Services in APEX

 Using RESTful Web Services in Oracle APEX allows developers to integrate external data and services seamlessly into their applications. RESTful services provide a flexible way to connect with a wide variety of APIs and data sources using standard HTTP methods such as GET, POST, PUT, and DELETE. By leveraging these web services, you can enhance your APEX applications with real-time data, third-party integrations, and interactive features without complex coding.

Using RESTful Web Services in Oracle APEX enables you to integrate external data and services into your applications easily and efficiently. RESTful Web Services communicate over HTTP using standard methods like GET, POST, PUT, and DELETE, allowing you to consume APIs or expose your own data to other applications. In APEX, you work with RESTful Web Services primarily through REST Data Sources, Web Source Modules, or by making explicit HTTP requests in PL/SQL or JavaScript.

To use RESTful Web Services in APEX, start by creating a REST Data Source or Web Source Module. This involves specifying the service endpoint URL, defining authentication if needed, and setting up request parameters. APEX then generates the necessary SQL or REST calls you can use as data sources in your regions, reports, or forms. You can also configure pagination and caching to optimize performance. For more advanced usage, you can call REST services directly using the APEX_WEB_SERVICE package in PL/SQL, which allows you to make HTTP calls, pass headers, and process responses programmatically.

Handling REST responses effectively requires parsing JSON or XML payloads. APEX provides built-in JSON parsing functions and utilities to convert REST data into usable rows or columns within your application. You can display this data in Interactive Reports, Classic Reports, Interactive Grids, or Cards. Additionally, APEX supports integrating REST services into dynamic actions or processes, enabling interactive features like live data updates or data submission.

Overall, using RESTful Web Services in APEX enhances your app’s capabilities by connecting it to a broad ecosystem of external resources. It simplifies data integration and allows you to build modern, connected applications with minimal custom coding. By understanding the configuration, calling mechanisms, and response handling, you can harness REST services to create robust, responsive Oracle APEX applications.

  1. Step 1: Define a Web Source

    • Go to the SQL Workshop in Oracle APEX.

    • Select RESTful Services or Web Source to create a new connection.

    • Define the REST API endpoint (e.g., https://earthquake.usgs.gov/fdsnws/event/1/query).

    • Define the Request Method (GET, POST, etc.) and set any necessary parameters (e.g., starttime, endtime).

  2. Step 2: Map Data to APEX

    • Once the Web Source is connected to the API, APEX will automatically generate data mappings for the API response.

    • You can define how the JSON or XML response should be parsed and how it will be presented in APEX components such as reports or charts.

  3. Step 3: Use in APEX Components

    • After the Web Source is created and data mappings are set up, you can use the data in APEX components like Reports, Forms, or Charts.

    • For instance, an Interactive Report can be connected to this Web Source, and data from the API will be displayed in the report.

  4. Step 4: Synchronize Data (Optional)

To periodically fetch or update the data, you can schedule jobs in APEX or use APEX RESTful Services to send or receive data on a regular basis.

What are the Best Practices for REST Synchronization:

  • Error Handling: Always handle potential errors, such as API downtime, incorrect data formats, and failed requests.

  • API Rate Limits: Be mindful of API rate limits imposed by the external service to avoid excessive requests.

  • Data Caching: Cache API responses when possible to improve performance and reduce the number of requests to the external API.

  • Security: Ensure secure communication by using HTTPS and handling authentication (e.g., OAuth, API keys) as needed.

Mastering the use of RESTful Web Services in APEX empowers you to build dynamic, responsive applications that communicate efficiently with external systems. Whether you are consuming public APIs or creating your own REST endpoints, APEX offers built-in tools to simplify configuration, testing, and data handling. By incorporating RESTful services, you ensure your applications stay modern, scalable, and capable of meeting evolving business needs.

How do I Test the REST Call

 Testing REST calls is a crucial step in developing and maintaining RESTful services in Oracle APEX. It ensures that your APIs function correctly, return the expected data, and handle errors gracefully. By thoroughly testing REST calls, you can verify the integrity, security, and performance of your web services before they are used in production environments. This blog will guide you through practical methods and tools for effectively testing REST calls in Oracle APEX.

Testing REST calls in Oracle APEX is an essential step to ensure that your RESTful services are working as expected. To test a REST call, you can use built-in APEX tools or external tools such as Postman or curl.

First, in Oracle APEX, you can navigate to Shared Components > RESTful Services. Select the RESTful Service module or specific REST endpoint you want to test. APEX provides a “Test” option where you can enter parameters if required and execute the REST call. The response, including the status code, headers, and body content, will be displayed in a readable format. This allows you to verify that the service returns the correct data and status.

If you prefer using external tools, Postman is highly recommended. With Postman, you can craft GET, POST, PUT, DELETE, or other HTTP requests to your REST endpoints. You set the URL, HTTP method, headers, and payload as needed. Sending the request shows you detailed response information, including the status, headers, and response body. This is useful for testing complex requests or simulating client interactions.

Alternatively, command-line tools like curl allow quick testing of REST endpoints from a terminal. For example, a simple curl command can send a GET request and display the response directly.

When testing REST calls, pay close attention to HTTP status codes such as 200 for success, 400 for bad requests, or 500 for server errors. Also, verify that response data matches expected formats, such as JSON or XML, and that any authentication or authorization requirements are correctly handled.

By thoroughly testing REST calls, you can identify and resolve issues early, ensure your APIs behave as intended, and provide a stable and secure integration within your Oracle APEX applications.

To test your RESTful service call:

  1. Enter an employee ID in the P1_EMPLOYEE_ID text box.

  2. Trigger the process (e.g., by clicking a button).

  3. Check the output, either in the page item or via the debug output in Oracle APEX.

Step by Step simplest method “Example”

Continuing from the previous section, create a “Module”

Enter the required fields.

A screenshot of a computer

AI-generated content may be incorrect.

Should look something like this:

A screenshot of a computer

AI-generated content may be incorrect.

Go back to the newly created Module

 

Lets add a handler

A screenshot of a computer

AI-generated content may be incorrect.

This handler will look for the id and return those rows. Add the SQL code below.

A screenshot of a computer

AI-generated content may be incorrect.

Navigating back to the module and slecteting the ID template displays the following

A red and white light on a black background

AI-generated content may be incorrect.

The new “ID” handler should look lik the following two images.

A screenshot of a computer

AI-generated content may be incorrect.

The new handler will display at the bottom of the screen.

A black rectangular object with yellow text

AI-generated content may be incorrect.

To test the call the Service use the following:

adbnphxadcdev01.adb.us-phoenix-1.oraclecloudapps.com/ords/xxdii_paul_test/airportslist/1

That will return the following from a browser:

Or the text equivalent of:

{"items":[{"id":1,"code":"0AK","name":"Pilot Station Airport","city":"Pilot Station","state":"AK"}],"hasMore":false,"limit":25,"offset":0,"count":1,"links":[{"rel":"self","href":"https://adbnphxadcdev01.adb.us-phoenix-1.oraclecloudapps.com/ords/xxdii_paul_test/airportslist/1"},{"rel":"describedby","href":"https://adbnphxadcdev01.adb.us-phoenix-1.oraclecloudapps.com/ords/xxdii_paul_test/metadata-catalog/airportslist/item"},{"rel":"first","href":"https://adbnphxadcdev01.adb.us-phoenix-1.oraclecloudapps.com/ords/xxdii_paul_test/airportslist/1"}]}

Common Scenarios for REST Synchronization:

  1. Fetching External Data

    • You can set up REST synchronization to regularly pull data from an external service (such as a weather API, earthquake data, or social media API) and display it in your APEX application.

  2. Sending Data to External Services

    • You can push data from your APEX forms to external systems (e.g., submitting user data to a CRM or payment gateway).

  3. Periodic Synchronization

    • Schedule periodic synchronization (e.g., every hour or day) to fetch or send data automatically without user intervention.

REST Synchronization in Oracle APEX: How It Works

Oracle APEX provides multiple ways to synchronize data with external RESTful services:

  1. Web Source Modules:

    • Web Source modules allow you to connect APEX to RESTful APIs and create Read-only data sources. You can use a Web Source to pull data from the external API and display it in your APEX reports or forms.

    • Example: If you want to display earthquake data from the USGS API in an APEX report, you would define a Web Source for the API endpoint and map the fields.

Steps to create a Web Source:

  • Create a Web Source in your APEX application.

  • Specify the REST API endpoint and request method (GET, POST, etc.).

  • Define how to parse the JSON or XML response and map it to APEX application columns.

  1. RESTful Web Services in APEX:

    • APEX allows you to expose RESTful Web Services that can be consumed by external applications or APIs. This is typically used when you want APEX to push data to another system via REST.

    • APEX provides RESTful Web Service endpoints, and you can define these services to accept or return data in JSON or XML format.

EXAMPLE:

  • APEX exposes a REST endpoint that an external system can send a POST request to, containing data that will be inserted into your APEX application's database.

  1. Interactive Grid with REST Integration:

    • You can use an Interactive Grid in APEX to integrate with REST APIs for CRUD operations (Create, Read, Update, Delete). The grid allows users to interact with remote data by making REST calls when records are added, edited, or deleted.

  2. Scheduled Jobs for Periodic Synchronization:

    • You can set up scheduled jobs in APEX (or Oracle DB) to periodically fetch or push data to external APIs. This can be useful for synchronizing data at regular intervals, without requiring manual intervention.

  3. Using PL/SQL to Interact with REST APIs:

    • For more custom control over the synchronization process, you can use PL/SQL to interact with RESTful APIs. The UTL_HTTP package in PL/SQL allows you to make HTTP requests to external REST services. You can use this to fetch data and then insert/update it in APEX.

In conclusion, testing REST calls is essential for delivering reliable and robust web services within Oracle APEX. By using tools such as Postman, curl, or built-in testing features, you can validate your API endpoints, troubleshoot issues, and improve the overall user experience. Regular and comprehensive testing helps prevent unexpected failures and ensures that your RESTful services meet both functional and security requirements.

HOW DO I CREATE A REST

 Creating a REST service is a fundamental skill for modern application development, enabling seamless communication between systems over the web. In Oracle APEX, building RESTful services allows you to expose database operations through standard HTTP methods, making your data accessible to external applications or integrating external data into your APEX apps. This blog will guide you through the essential steps to create a REST service, covering key concepts and practical implementation details to get your REST endpoints up and running efficiently.

Creating a REST service in Oracle APEX involves several steps that enable your database to interact with external applications via HTTP requests. REST (Representational State Transfer) is an architectural style that uses standard HTTP methods like GET, POST, PUT, and DELETE to perform CRUD operations on resources. In Oracle APEX, you can create RESTful services using Oracle REST Data Services (ORDS), which acts as a middle tier between the database and web clients.

To start, you first need to define a RESTful service module in ORDS. This module serves as a container for related REST endpoints. Within the module, you create RESTful templates, which define the URI patterns for your API. Each template can have multiple handlers that specify the HTTP methods supported (GET, POST, etc.) and the SQL or PL/SQL code that executes when those endpoints are called. For example, a GET handler can retrieve data from a table, while a POST handler can insert new records.

The process begins in Oracle APEX or directly in ORDS by navigating to the RESTful Services section. You create a new module and define its base path. Then, you add templates corresponding to specific resource paths and associate handlers with each template. When writing SQL or PL/SQL in handlers, you leverage bind variables and APEX collections to handle input parameters and request bodies.

Security is an important consideration; you can configure authentication and authorization schemes to protect your REST services. Oracle APEX supports OAuth2, Basic Authentication, and other methods to ensure that only authorized users or applications can access your APIs.

Once your REST service is created and published, it becomes accessible via URLs that external applications can call. You can test your REST endpoints using tools like Postman or curl to verify they behave as expected. By organizing your RESTful services into modules, templates, and handlers, Oracle APEX provides a flexible and powerful framework for creating APIs that integrate your database with other systems efficiently and securely.

Example

Step 1. Navigate to an existing REST Data Service and click on the “Manage Synchronization” link.

A screenshot of a computer

AI-generated content may be incorrect.

Step 2. Select the “New Table” option, provide a name for the table, and press “Save”

A screenshot of a computer

AI-generated content may be incorrect.

Step 3. Click on the “Create Table” in the “Table Status” region.

A screenshot of a computer

AI-generated content may be incorrect.

Step 4. Review the table in SQL workshop

A screenshot of a computer

AI-generated content may be incorrect.

Step 5. Select the type of  “data load” that you want by selecting from the “Append/Merge/Replace” buttons

A screenshot of a computer

AI-generated content may be incorrect.

Step 6. Press the “Save and Run” button.

You should have the following results in your table.

A screenshot of a computer

AI-generated content may be incorrect.

By mastering the creation of REST services in Oracle APEX, you unlock powerful integration capabilities that extend the reach of your applications. Whether you are building APIs for mobile apps, third-party systems, or internal services, understanding how to create and manage REST endpoints will enhance your development toolkit and improve data accessibility and interoperability across platforms.

HOW DO I SET A HOME PAGE

 Setting a home page in Oracle APEX is an essential step in defining the default landing page for your application. The home page serves as ...