Skip to main content

Working with JSON in Oracle APEX Using PL/SQL

Updated 4 min readMarkdown
Working with JSON in Oracle APEX Using PL/SQL

JSON has become a regular part of application development, especially when working with REST APIs and external systems. In Oracle APEX, we often receive JSON responses from external services, process request payloads, or generate JSON data for AJAX callbacks. Oracle Database provides several ways to work with JSON directly from SQL and PL/SQL. In this article, let's look at a few practical examples.

  1. Create a JSON object Oracle provides the JSON_OBJECT_T type for working with JSON objects in PL/SQL. For example, suppose we need to create a JSON response containing employee information. DECLARE l_json JSON_OBJECT_T; BEGIN l_json := JSON_OBJECT_T();

    l_json.put('employee_id', 101); l_json.put('employee_name', 'John Smith'); l_json.put('department', 'Finance');

    DBMS_OUTPUT.PUT_LINE(l_json.to_string); END; The output will look like: { "employee_id": 101, "employee_name": "John Smith", "department": "Finance" } This is useful when building JSON payloads for external API requests.

  2. Parse a JSON response Now consider the opposite situation. An external API returns the following JSON response: { "transaction_id": 5001, "status": "SUCCESS", "amount": 2500 } We can parse it using JSON_OBJECT_T. DECLARE l_response CLOB; l_json JSON_OBJECT_T; l_id NUMBER; l_status VARCHAR2(50); l_amount NUMBER; BEGIN l_response := '{ "transaction_id": 5001, "status": "SUCCESS", "amount": 2500 }';

    l_json := JSON_OBJECT_T.parse(l_response);

    l_id := l_json.get_number('transaction_id'); l_status := l_json.get_string('status'); l_amount := l_json.get_number('amount');

    DBMS_OUTPUT.PUT_LINE('Transaction ID: ' || l_id); DBMS_OUTPUT.PUT_LINE('Status: ' || l_status); DBMS_OUTPUT.PUT_LINE('Amount: ' || l_amount); END; The parse method converts the JSON text into a PL/SQL JSON object. We can then retrieve individual values using the appropriate getter methods.

  3. Handle nested JSON objects API responses are not always flat. They often contain nested objects. Example: { "transaction_id": 5001, "customer": { "customer_id": 200, "customer_name": "Alex" } } We can access the nested customer object like this: DECLARE l_json JSON_OBJECT_T; l_customer JSON_OBJECT_T; BEGIN l_json := JSON_OBJECT_T.parse( '{ "transaction_id": 5001, "customer": { "customer_id": 200, "customer_name": "Alex" } }' );

    l_customer := l_json.get_object('customer');

    DBMS_OUTPUT.PUT_LINE( l_customer.get_string('customer_name') ); END; This is particularly useful when integrating with APIs that return customer, payment, invoice, or order information in nested structures.

  4. Convert JSON arrays into relational rows using JSON_TABLE One of the most useful SQL features for JSON processing is JSON_TABLE. Suppose an API returns multiple employees: { "employees": [ { "employee_id": 101, "employee_name": "John" }, { "employee_id": 102, "employee_name": "Alex" } ] } We can convert the array into rows using: SELECT jt.employee_id, jt.employee_name FROM JSON_TABLE( '{ "employees": [ {"employee_id": 101, "employee_name": "John"}, {"employee_id": 102, "employee_name": "Alex"} ] }', '\(.employees[*]' COLUMNS ( employee_id NUMBER PATH '\).employee_id', employee_name VARCHAR2(100) PATH '$.employee_name' ) ) jt; The result is a normal relational result set. This is useful when processing API responses and inserting multiple records into database tables.

  5. Generate JSON using SQL You don't always need PL/SQL object types to create JSON. Oracle SQL provides JSON_OBJECT and JSON_ARRAYAGG. For example: SELECT JSON_OBJECT( 'employee_id' VALUE employee_id, 'employee_name' VALUE employee_name, 'department' VALUE department RETURNING CLOB ) AS employee_json FROM employees; To generate an array of employee records: SELECT JSON_ARRAYAGG( JSON_OBJECT( 'employee_id' VALUE employee_id, 'employee_name' VALUE employee_name ) RETURNING CLOB ) AS employees_json FROM employees; These functions are useful when building API responses or passing structured data to JavaScript.

  6. Use JSON in Oracle APEX AJAX callbacks JSON is also useful when an APEX page needs to communicate with a server-side PL/SQL process without submitting the entire page. For example, an AJAX Callback can return a JSON response. BEGIN apex_json.open_object;

    apex_json.write('status', 'SUCCESS'); apex_json.write('message', 'Data processed successfully');

    apex_json.close_object; END; On the JavaScript side: apex.server.process( "GET_RESPONSE", {}, { dataType: "json", success: function (data) { console.log(data.status); console.log(data.message); } } ); Create a page-level Ajax Callback process named GET_RESPONSE. This approach is useful for refreshing part of a page, displaying messages, and updating items without a full page submission. Things to watch out for Check whether a JSON property exists before reading it when the response structure can vary. Handle missing values and JSON nulls carefully. Use CLOB for large JSON payloads. Validate external JSON before processing it. Handle malformed JSON with appropriate exception handling. Be aware of your Oracle Database version when using native JSON features. Final thoughts JSON support in Oracle Database makes API integration and structured data processing much easier. For simple JSON objects, JSON_OBJECT_T is convenient. For converting arrays into rows, JSON_TABLE is especially useful. For generating JSON from database queries, SQL/JSON functions are often the simplest option.

1 view

About the author

  • I am an Oracle APEX Developer with 2+ years of experience in building interactive, user-friendly, and scalable web applications. I work primarily with Oracle APEX, SQL, and PL/SQL, along with HTML and CSS to create responsive and efficient application interfaces. I enjoy solving business problems through technology and continuously learning new features and best practices in the Oracle ecosystem.

No comments yet. Be the first to comment.

More from this blog