Parsing JSON data into Oracle Database tables can be achieved efficiently using built-in SQL/JSON functions, PL/SQL object types, or external client tools. Oracle provides comprehensive capabilities to transform hierarchical JSON strings into traditional relational rows and columns. 1. Primary SQL Methods (Declarative Approach)
This is the fastest and most efficient way to query and map JSON directly to tables using standard SQL statements. JSON_TABLE (Recommended)
The JSON_TABLE function acts as a virtual relational table, automatically splitting JSON arrays into individual rows and extracting nested values into separate columns.
Best Use Case: When you need to process large JSON documents containing object arrays or complex hierarchies. Example:
INSERT INTO employees_table (emp_id, emp_name, department) SELECT jt.id, jt.name, jt.dept FROM json_staging_table j, JSON_TABLE(j.json_content, ‘\(.employees[*]' COLUMNS ( id NUMBER PATH '\).id’, name VARCHAR2(50) PATH ‘\(.name', dept VARCHAR2(50) PATH '\).dept’ ) ) jt; Use code with caution. Simple Dot Notation
If the database column is validated as well-formed JSON (using an IS JSON check constraint or the native JSON data type), you can bypass complex functions and read attributes natively.
Best Use Case: Quick extraction of flat, non-array scalar values. Example:
SELECT j.json_content.employees.name FROM json_staging_table j; Use code with caution. 2. PL/SQL Object Types (Programmatic Approach)
For complex multi-step data pipelines, API orchestrations, or scenarios requiring custom processing logic, Oracle provides object-oriented PL/SQL wrappers. JSON_ELEMENT_T, JSON_OBJECT_T, and JSON_ARRAY_T
These object types allow you to programmatically parse, traverse, inspect, and mutate JSON trees directly inside memory arrays.
Best Use Case: When JSON documents feature multi-tiered conditional data that requires data validation loops before insertion. Example:
DECLARE l_json JSON_OBJECT_T; l_emp_name VARCHAR2(100); BEGIN – Parse the raw JSON text string into a structural object l_json := JSON_OBJECT_T.parse(‘{“id”:101, “name”:“John Doe”}’); l_emp_name := l_json.get_string(‘name’); INSERT INTO employees_table (emp_name) VALUES (l_emp_name); END; Use code with caution. 3. Native Tools and External Loading Methods
If your JSON data resides outside the database environment (e.g., local logs, cloud file systems), you can stream it using native tools. Converting JSON Data to Relational Data in Oracle 12C
Leave a Reply