Modern applications often need the flexibility of JSON documents and the reliability of relational databases. A web application may prefer working with nested JSON objects, while reporting services, financial systems, and enterprise applications may depend on relational tables, SQL queries, joins, and constraints.
Traditionally, developers have had to choose between these approaches or build additional application logic to connect them. A common solution is to store data in relational tables and create separate APIs that convert database rows into JSON documents. However, this introduces extra mapping code and can create synchronization challenges when multiple applications access the same information.
Oracle JSON Relational Duality Views address this problem by allowing applications to work with relational data as JSON documents without storing a separate copy of those documents. The data remains in relational tables, but Oracle Database presents it through a JSON document interface. Depending on the view configuration, applications can also insert, update, and delete data through that JSON interface.
This article explains how JSON Relational Duality Views work, how to create them using SQL, how to represent related tables as nested JSON documents, and how to perform read and write operations while maintaining relational consistency.
What Are Oracle JSON Relational Duality Views?
JSON Relational Duality Views are database views that expose relational table data as JSON documents. They provide two ways to work with the same underlying data:
- Relational approach: Applications access normalized tables using SQL.
- Document approach: Applications access the same information as hierarchical JSON documents.
The important point is that Oracle does not maintain a separate JSON copy of the relational data for the duality view. JSON documents are generated when the view is queried.
For example, consider an e-commerce database containing three tables:
- Customers: Stores customer details.
- Orders: Stores order information.
- Order_Items: Stores products associated with each order.
A relational application can query these tables using joins. A document-oriented application may prefer a structure such as:
{
"_id": 1001,
"customerName": "John Smith",
"email": "[email protected]",
"orders": [
{
"orderId": 501,
"orderDate": "2026-12-01",
"items": [
{
"productName": "Laptop",
"quantity": 1,
"price": 1200
},
{
"productName": "Mouse",
"quantity": 2,
"price": 25
}
]
}
]
}
Without duality views, developers might need to write queries, map the results into nested objects, and implement separate update logic.
With a JSON Relational Duality View, Oracle can generate this document structure from the underlying relational tables. Applications can use the document interface while other applications continue working with the relational tables.
How JSON Relational Duality Works
A duality view connects relational tables and JSON documents through a declarative database definition.
The database uses table relationships and identifying columns to determine how relational rows map to JSON objects and arrays.
The main components are:
- Root table: Defines the top-level object of the JSON document.
- Related tables: Provide nested objects or arrays.
- Primary or unique keys: Identify the rows represented in the document.
- JSON mapping: Defines how relational columns become JSON fields.
- Updatability annotations: Specify whether applications can insert, update, or delete through the view.
A one-to-many relationship generally becomes a nested JSON array. A one-to-one or many-to-one relationship can be represented within the same JSON object using the appropriate mapping.
For example, one customer can have multiple orders. The customer becomes the root object, while the orders become a nested array.
The same customer and order records remain available through ordinary SQL queries. Changes made through an updatable duality view are reflected in the underlying relational tables.
Creating Relational Tables for a Duality View
Let's build a simple customer and order management example.
The application needs to store customer information and their orders. Start by creating two relational tables.
Creating the customers table
CREATE TABLE customers
(
customer_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
customer_name VARCHAR2(100) NOT NULL,
email VARCHAR2(150) NOT NULL,
city VARCHAR2(100),
CONSTRAINT pk_customers PRIMARY KEY (customer_id),
CONSTRAINT uk_customers_email UNIQUE (email)
);
The customers table stores the basic information for each customer. The customer_id column is the primary key, while the email column has a unique constraint.
Creating the orders table
CREATE TABLE customer_orders
(
order_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
customer_id NUMBER NOT NULL,
order_date DATE DEFAULT SYSDATE,
order_status VARCHAR2(30),
total_amount NUMBER(12, 2),
CONSTRAINT pk_customer_orders PRIMARY KEY (order_id),
CONSTRAINT fk_orders_customer
FOREIGN KEY (customer_id)
REFERENCES customers(customer_id)
);
The customer_orders table stores order information. Its customer_id column references the customers table, establishing a one-to-many relationship.
One customer can have multiple orders, but each order belongs to one customer.
Inserting sample data
Insert a few customer records:
INSERT INTO customers
(customer_name, email, city)
VALUES
('John Smith', '[email protected]', 'New York');
INSERT INTO customers
(customer_name, email, city)
VALUES
('Sarah Johnson', '[email protected]', 'London');
Insert some orders:
INSERT INTO customer_orders
(customer_id, order_status, total_amount)
VALUES
(1, 'Completed', 1200.00);
INSERT INTO customer_orders
(customer_id, order_status, total_amount)
VALUES
(1, 'Processing', 450.00);
INSERT INTO customer_orders
(customer_id, order_status, total_amount)
VALUES
(2, 'Completed', 850.00);
COMMIT;
The relational database now contains customer and order information in separate tables.
Creating a JSON Relational Duality View
Oracle provides the CREATE JSON RELATIONAL DUALITY VIEW statement to define a JSON document interface over relational tables.
The following example creates a view that exposes customer information as JSON documents.
CREATE JSON RELATIONAL DUALITY VIEW customer_dv AS
SELECT JSON
{
'_id' : c.customer_id,
'customerName' : c.customer_name,
'email' : c.email,
'city' : c.city
}
FROM customers c WITH UPDATE INSERT DELETE;
The JSON object defines how relational columns map to document fields.
The id field is important because every duality view document must have a top-level document identifier. In this example, it maps to the customerid primary key.
The WITH UPDATE INSERT DELETE annotations make the view updatable. Applications can perform the supported operations through the view, and Oracle applies the corresponding changes to the underlying table.
Without these updatability annotations, a duality view is read-only by default.
Creating Nested JSON Documents
The previous view exposes customer information, but it does not include orders.
A document-oriented application may need the customer and all associated orders in a single JSON object.
Oracle supports nested JSON generation using subqueries inside the JSON document definition.
Create a new view that includes an orders array:
CREATE JSON RELATIONAL DUALITY VIEW customer_orders_dv AS
SELECT JSON
{
'_id' : c.customer_id,
'customerName' : c.customer_name,
'email' : c.email,
'city' : c.city,
'orders' :
[
SELECT JSON
{
'orderId' : o.order_id,
'orderDate' : o.order_date,
'orderStatus' : o.order_status,
'totalAmount' : o.total_amount
}
FROM customer_orders o
WHERE o.customer_id = c.customer_id
]
}
FROM customers c WITH UPDATE INSERT DELETE;
The inner query retrieves the orders associated with the current customer.
The JSON array syntax creates a nested collection of order objects. A customer with multiple orders receives multiple objects inside the orders array.
The resulting document has a structure similar to this:
{
"_id": 1,
"customerName": "John Smith",
"email": "[email protected]",
"city": "New York",
"orders": [
{
"orderId": 1,
"orderStatus": "Completed",
"totalAmount": 1200
},
{
"orderId": 2,
"orderStatus": "Processing",
"totalAmount": 450
}
]
}
This structure is generated from the customers and customer_orders tables. The application does not need to store a separate copy of the nested document.
The example demonstrates the basic one-to-many mapping. For production use, ensure that all underlying tables have the identifying keys required by the view definition and that the intended update behavior is supported.
Querying JSON Documents
After creating a duality view, applications can query it like a database object.
A simple SELECT statement retrieves the JSON documents:
SELECT DATA
FROM customer_orders_dv;
Each row contains a JSON document in the DATA column.
To retrieve a particular customer, use the document identifier:
SELECT DATA
FROM customer_orders_dv
WHERE JSON_VALUE(DATA, '$._id') = '1';
Applications can also extract individual JSON fields using Oracle SQL/JSON functions.
For example, retrieve customer names and email addresses:
SELECT
JSON_VALUE(DATA, '$.customerName') AS customer_name,
JSON_VALUE(DATA, '$.email') AS email
FROM customer_orders_dv;
This approach allows SQL applications to work with JSON document fields without manually parsing the complete document.
The duality view has one visible payload column named DATA, which contains the generated JSON object.
Updating Data Through a Duality View
One of the important capabilities of JSON Relational Duality Views is that applications can modify relational data through the JSON interface when the view permits those operations.
For example, an application can update a customer's city by modifying the JSON document.
UPDATE customer_orders_dv
SET DATA = JSON_TRANSFORM(
DATA,
SET '$.city' = 'Boston'
)
WHERE JSON_VALUE(DATA, '$._id') = '1';
COMMIT;
Oracle maps the changed JSON field back to the corresponding relational column.
The underlying customers table now contains Boston as the customer's city.
The application does not need to issue a separate UPDATE statement against the customers table.
This is useful when different services use different data access approaches. One service can work with JSON documents, while another can continue using ordinary relational SQL.
Inserting a New JSON Document
An application can also insert a document through an updatable duality view.
For example:
INSERT INTO customer_orders_dv (DATA)
VALUES (
JSON {
'customerName' : 'Michael Brown',
'email' : '[email protected]',
'city' : 'Chicago'
}
);
COMMIT;
Oracle maps the document fields to the corresponding columns in the customers table.
The customer_id column is generated by the identity definition. The new customer becomes available through both the relational table and the duality view.
For nested documents, insert behavior depends on the view's updatability configuration, table relationships, and constraints. Applications should test the complete document structure before using nested inserts in production.
Understanding ETAG and Optimistic Concurrency
When multiple users or services update the same document, one application may overwrite changes made by another application.
Consider a customer document retrieved by two services:
- Service A retrieves the customer's current document.
- Service B retrieves the same document.
- Service A changes the city and saves the document.
- Service B changes the email and attempts to save its older document.
Without concurrency protection, Service B could overwrite Service A's changes depending on the update operation.
Oracle duality views provide an automatically maintained ETAG value to support optimistic concurrency control.
The ETAG represents the state of the document. When an application retrieves a document, it receives the current ETAG in the document's _metadata object.
If the document changes before the application writes its update, the database can detect that the submitted version is stale.
An update using an outdated ETAG is rejected rather than silently overwriting newer data.
The application can then retrieve the latest document, apply its intended change, and retry the update.
This approach is useful for web applications, APIs, and distributed services where multiple clients may update the same business object.
By default, columns contributing to the document are included in ETAG calculation. The CHECK and NOCHECK ETAG annotations allow developers to control which columns participate.
Controlling Insert, Update, and Delete Operations
Not every application should have permission to modify every part of a document.
Oracle provides annotations that control which operations are allowed.
Annotation | Purpose |
|---|
WITH INSERT | Allows insertion through the view |
WITH UPDATE | Allows updates through the view |
WITH DELETE | Allows deletion through the view |
WITH NOINSERT | Prevents insertion |
WITH NOUPDATE | Prevents updates |
WITH NODELETE | Prevents deletion |
WITH CHECK ETAG | Includes specified data in ETAG calculation |
WITH NOCHECK ETAG | Excludes specified data from ETAG calculation |
These annotations can be applied at the table level and, where supported, at the column level.
For example, an application may need to update customer contact information but should not be allowed to modify an internal customer identifier.
A view can allow updates while marking the identifier column as non-updatable.
CREATE JSON RELATIONAL DUALITY VIEW customer_read_update_dv AS
SELECT JSON
{
'_id' : c.customer_id,
'customerName' : c.customer_name,
'email' : c.email
}
FROM customers c WITH UPDATE;
The view exposes the selected fields and allows supported updates. The document identifier remains the identifying field for the root table.
For more restrictive requirements, use column-level annotations and database privileges to control the exact operations available to each application.
Working With the Same Data Through SQL and JSON
The main advantage of duality views is that applications can use different data models without maintaining separate copies.
For example, an existing reporting application can continue querying the relational tables:
SELECT
c.customer_name,
c.email,
o.order_id,
o.total_amount
FROM customers c
JOIN customer_orders o
ON c.customer_id = o.customer_id;
A new application can retrieve the same customer information through the JSON view:
SELECT DATA
FROM customer_orders_dv;
If an application updates the customer city through the duality view, the relational table reflects that change. If another application updates the underlying relational table, the next query against the duality view reflects the updated value.
There is one shared source of truth rather than separate relational and document copies that need synchronization.
Best Practices for Production Applications
JSON Relational Duality Views simplify application development, but they still require careful database design.
Design relational tables before defining document structures
Identify entities, relationships, primary keys, and unique constraints before creating the view.
The relational model remains the foundation of the data. A well-designed relational schema makes it easier to create reliable JSON documents and maintain consistency.
Use meaningful document identifiers
Every document needs a top-level _id field that identifies its root table row.
Use a primary key or another appropriate unique identifier. Avoid identifiers that can change frequently because they can complicate document updates and references.
Keep document structures focused
Do not create extremely large JSON documents containing every related record in the database.
Include the data that an application needs for a particular business operation. For example, a customer profile view may include recent orders, while an administrative reporting view may use a different document structure.
Multiple duality views can expose different JSON hierarchies over the same underlying tables.
Configure updatability explicitly
A view is read-only by default. Enable only the operations required by the application.
Use table-level and column-level annotations to prevent applications from changing protected fields.
Use ETAG for concurrent updates
Applications that retrieve documents, modify them locally, and save them later should account for concurrent changes.
Use ETAG-based optimistic concurrency to detect stale updates and avoid silently overwriting newer data.
Protect database access
Duality views do not replace authentication or authorization.
Grant applications only the database privileges required for their operations. Use separate database accounts or roles for read-only services, document update services, and administrative workloads.
Test nested document operations
Nested JSON structures can represent one-to-many and many-to-many relationships. Test inserts, updates, and deletes against realistic data before deploying the view.
Pay particular attention to how changes to nested objects map to their underlying relational rows.
Common Mistakes to Avoid
One common mistake is assuming that a duality view stores a second copy of JSON documents. The JSON documents are generated on demand from the underlying relational data.
Another mistake is forgetting the _id field. A duality view requires a document identifier that maps to the identifying columns of the root table.
Developers sometimes assume that every duality view supports inserts, updates, and deletes automatically. Views are read-only by default. Updatability must be configured using the appropriate annotations, and the underlying table relationships must support the requested operations.
Ignoring ETAG handling is another problem. Applications that update documents using stale data may encounter conflicts. They should retrieve the latest document and retry when an optimistic concurrency check fails.
Finally, do not assume that a nested JSON document removes the need for relational database design. Primary keys, unique constraints, data types, and relationships remain important because the data is still stored in relational tables.
Advantages and Disadvantages
JSON Relational Duality Views provide several advantages:
- Allow applications to access relational data as JSON documents.
- Avoid maintaining separate copies of relational and document data.
- Support read and write operations through configured views.
- Map one-to-many relationships into nested JSON arrays.
- Keep SQL-based applications compatible with the same underlying data.
- Provide ETAG-based optimistic concurrency support.
- Allow multiple document structures over shared relational tables.
However, developers should also consider the limitations:
- Require a compatible Oracle Database version and supported configuration.
- Need appropriate identifying keys in the underlying tables.
- Have restrictions on supported table structures and operations.
- Require careful updatability configuration.
- Can produce large documents if too many related records are included.
- Need application-level handling for concurrency conflicts.
- Do not remove the need for relational database security and authorization.
Conclusion
Oracle JSON Relational Duality Views provide a practical way to combine relational database storage with document-oriented application development. Data remains in normalized relational tables, while applications can access it through JSON documents without maintaining a separate document store.
Developers can create views over one or more related tables, represent relationships as nested JSON objects and arrays, and perform supported insert, update, and delete operations through the document interface.
For enterprise applications, this approach can reduce data mapping code and allow existing SQL systems to work alongside newer JSON-based services. By designing the underlying tables carefully, configuring updatability, and using ETAG for optimistic concurrency, developers can build applications that benefit from both relational consistency and JSON flexibility.
Join the conversation! Your thoughts help the community grow.