Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • Find Time Consuming SQL Statements in Oracle 10g Oracle
  • To find explain plan for a statement that occurred in past. Oracle
  • tar and untar a dolder with all its subfolder. Linux/Unix
  • Does DBMS_JOB recompute the NEXT_DATE interval after or before Oracle
  • Which environment is used by currently running process ( Very good) Linux/Unix
  • DBMS_Shared_pool pinning triggers Oracle
  • Sort with ASCII order and Numeric Order Linux/Unix
  • To Find Orphan OS processes. Linux/Unix
  • Kernel Parameters for Solaris Linux/Unix
  • Oracle Data Direct to TAPE Oracle
  • Specify the Rollback segment to use in Transaction Oracle
  • How To Limit The Access To The Database So That Only One User Per Schema Are Connected (One Concurrent User Per Schema) Oracle
  • EXTPROC Oracle
  • load SPM baseline from cursor cache Oracle
  • Configure ssh authentications for RAC Oracle

JSON/XML Types in Oracle

Posted on 18-Mar-202518-Mar-2025 By Admin No Comments on JSON/XML Types in Oracle

Handling JSON and XML Data in Oracle

Oracle provides built-in support for JSON and XML data storage, querying, and manipulation. Below are methods to handle both data types efficiently.


1. Handling JSON Data in Oracle

A. Storing JSON Data

  • JSON can be stored in:
    • CLOB (Character Large Object)
    • VARCHAR2 (for small JSON documents)
    • BLOB
    • Native JSON Data Type (from Oracle 21c)

Example: Creating a Table with JSON Column

CREATE TABLE employees (
    id NUMBER PRIMARY KEY,
    name VARCHAR2(100),
    details CLOB CHECK (details IS JSON) -- Enforce JSON format
);

B. Inserting JSON Data

INSERT INTO employees VALUES (1, 'John Doe', '{"age": 30, "role": "Developer"}');

C. Querying JSON Data

1. Extracting JSON Values Using JSON_VALUE

SELECT name, JSON_VALUE(details, '$.age') AS age
FROM employees
WHERE JSON_VALUE(details, '$.role') = 'Developer';
  • $.age → Extracts age field.
  • $.role → Filters JSON objects where role = Developer.

2. Querying JSON Data Using JSON_TABLE

SELECT emp_id, jt.age, jt.role
FROM employees,
     JSON_TABLE(details, '$' COLUMNS (age NUMBER PATH '$.age', role VARCHAR2(100) PATH '$.role')) jt;
  • Converts JSON fields into relational table format.

D. Updating JSON Data

UPDATE employees 
SET details = JSON_TRANSFORM(details, SET '$.age' = 35)
WHERE id = 1;
  • JSON_TRANSFORM modifies JSON fields without rewriting the entire JSON document.

E. Indexing JSON Data

For faster retrieval, create a JSON Search Index:

CREATE INDEX emp_json_idx ON employees (details) INDEXTYPE IS JSON;

2. Handling XML Data in Oracle

A. Storing XML Data

XML can be stored in:

  • CLOB
  • XMLType (Oracle’s optimized XML storage type)

Example: Creating a Table with XML Column

CREATE TABLE orders (
    order_id NUMBER PRIMARY KEY,
    order_details XMLTYPE
);

B. Inserting XML Data

INSERT INTO orders VALUES (101, XMLTYPE('<order><customer>John</customer><total>100</total></order>'));

C. Querying XML Data

1. Extracting XML Data Using EXTRACTVALUE

SELECT order_id, EXTRACTVALUE(order_details, '/order/customer') AS customer
FROM orders;
  • Retrieves customer name from the XML structure.

2. Using XMLTABLE for More Complex Queries

SELECT o.order_id, x.customer, x.total
FROM orders o,
     XMLTABLE('/order' PASSING o.order_details
              COLUMNS customer VARCHAR2(100) PATH 'customer',
                      total NUMBER PATH 'total') x;
  • Converts XML fields into relational table format.

D. Updating XML Data

Using XMLQuery and XMLModify:

UPDATE orders
SET order_details = XMLModify(order_details, 'replace value of (/order/total/text())[1] with "150"')
WHERE order_id = 101;
  • Updates the total amount in the XML.

E. Indexing XML Data

For fast lookups, create an XML Index:

CREATE INDEX order_xml_idx ON orders (order_details) INDEXTYPE IS XDB.XMLINDEX;

3. JSON vs. XML in Oracle

FeatureJSONXML
StorageCLOB, VARCHAR2, BLOB, JSON TypeXMLType, CLOB
Query LanguageSQL with JSON functionsXPath, XQuery
PerformanceFaster for small, simple documentsBetter for hierarchical, large data
IndexingJSON Index (JSON_VALUE, JSON_TABLE)XML Index (XMLIndex)

Conclusion

  • Use JSON for lightweight, structured data (API responses, logging).
  • Use XML when hierarchical structure and schema validation are needed.
  • Indexes improve performance for querying both JSON and XML.
  • Conversion Functions (JSON_TABLE, XMLTABLE) help integrate them with relational queries.

Oracle

Post navigation

Previous Post: CPU Core related projections
Next Post: How to connect to Oracle Database with Wallet with Python.

Related Posts

  • Vivek Tuning for Row Locks. Oracle
  • Jai Shree Ram Oracle
  • On solaris 10, “S” link is not part of $ORACLE_HOME/bin/oracle as default. ( For 9.2.0.8) Oracle
  • Locktree.sql Oracle
  • DBA_HIST_SQLSTAT contents Oracle
  • create user with unlimited quota Oracle

Leave a Reply Cancel reply

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

Categories

  • Ansible (0)
  • AWS (2)
  • Azure (1)
  • Django (0)
  • GIT (1)
  • Linux/Unix (149)
  • MYSQL (5)
  • Oracle (395)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (1)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (17)
  • rman-dataguard (26)
  • shell (150)
  • SQL scripts (343)
  • SQL Server (6)
  • Uncategorized (0)
  • Videos (0)

Recent Posts

  • prepfiles.sh for step by step generating pending statistics files10-Mar-2026
  • tracksqltime.sql05-Mar-2026
  • Complete Git Tutorial for Beginners25-Dec-2025
  • Postgres DB user and OS user.25-Dec-2025
  • Trace a SQL session from another session using ORADEBUG30-Sep-2025
  • SQL Server Vs Oracle Architecture difference25-Jul-2025
  • SQL Server: How to see historical transactions25-Jul-2025
  • SQL Server: How to see current transactions or requests25-Jul-2025
  • T-SQL Vs PL/SQL Syntax25-Jul-2025
  • Check SQL Server edition25-Jul-2025

Archives

  • 2026
  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • My FTP Job Scheduling for www.pvmehta.com PHP/MYSQL/Wordpress
  • Load SPM baseline from AWR Oracle
  • sess1.sql Oracle
  • currwaitobj.sql SQl_ID and SQL statement you can get from currwaitobj.sql Oracle
  • How to find pinned objects from shared pool. (pinned via dbms_shared_pool.keep) Oracle
  • backspace in SQL Plus not working then..? Linux/Unix
  • RAC with RHEL4 and 11g Oracle
  • sbind.sql Find Bind variable from sql_id sqlid Oracle

Copyright © 2026 pvmehta.com.

Powered by PressBook News WordPress theme