Sub topic 1
Why JSON in a Relational Database
Oracle Database can store both structured relational data and JSON documents. This combination is useful when an application receives data whose fields vary, while the database still needs transactions, security, SQL queries, and reliable storage.
Prerequisites
You should know basic SQL, including SELECT, INSERT, UPDATE, joins, and GROUP BY. You should also understand primary keys, foreign keys, and the JSON format of objects, arrays, and scalar values. The examples use Oracle Database 21c or later. JSON-relational duality views require Oracle Database 23ai.
Why store JSON in Oracle?
JSON is a text-based format that represents data as objects and arrays. It is common in web APIs, event messages, configuration files, and application records. A single JSON document can contain nested information such as a customer, an order, and its line items.
A relational table stores data in columns with defined types. This structure makes validation, joins, and reporting straightforward. However, changing the table every time an external document gains a field can be inconvenient.
JSON provides flexibility. Oracle provides relational database features around that JSON:
- Transactions keep related changes consistent.
- SQL can filter and extract values from documents.
- Constraints and indexes can protect and speed up access.
- Security controls can limit who reads or changes data.
- Relational tables can remain the authoritative source for highly structured data.
The important design question is not whether JSON or tables are universally better. It is which representation best matches each part of the workload.
A working model
Think of JSON as a document stored inside a row. The row can have ordinary relational columns, a JSON column, or both.
CREATE TABLE event_log (
event_id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
created_at TIMESTAMP DEFAULT SYSTIMESTAMP NOT NULL,
payload JSON
);
INSERT INTO event_log (payload)
VALUES (
'{"eventType":"login",
"user":{"id":42,"name":"Ada"},
"success":true}'
);In Oracle Database 21c and later, JSON is a native data type. Oracle validates that assigned values are valid JSON. On older releases, or when compatibility requires it, a VARCHAR2, CLOB, or BLOB column can hold JSON text with an IS JSON constraint.
| Modeling choice | Best fit | Main cost |
|---|---|---|
| Relational columns and tables | Stable fields, joins, and reporting | Schema changes require planning |
| JSON column | Variable or externally shaped documents | Queries and constraints need JSON features |
| Duality view | One data model needed as both rows and documents | Requires supported Oracle features and updatability rules |
How JSON fits with relational design
Use ordinary columns for values that are central to relationships, constraints, or frequent searches. For example, an order identifier, customer identifier, and order status may deserve relational columns when they drive joins and reports.
Use a JSON column for data that is optional, nested, or controlled by an external format. A product-specific set of attributes is a common example. The database can still query those attributes with SQL/JSON path expressions.
Oracle also supports a third approach: a JSON-relational duality view. The underlying data remains in normalized tables, while the view presents related rows as one document. This lets a service read and write document-shaped data without giving up relational storage.
The choice depends on access patterns:
- Choose relational tables when data has stable structure and many relationships.
- Choose JSON columns when documents arrive as units and their structure changes.
- Choose a duality view when different clients need the same data as rows and as documents.
The next sections build the working skills for these choices: storing JSON safely, addressing fields with paths, extracting values, flattening arrays, indexing documents, and exposing relational tables through duality views.
Exercise
Which model is usually the better starting point for data with stable fields, many foreign-key relationships, and frequent joins?
Show answer
Relational tables, because their columns, keys, constraints, and joins directly represent that workload.