Sub topic 2
Storing documents in JSON columns
This chapter assumes basic SQL, primary keys, and foreign keys. You should also know that a JSON document contains objects, arrays, and scalar values such as strings, numbers, booleans, and null.
Oracle Database can store a JSON document in a native JSON column or in a character or binary column protected by an IS JSON constraint. The native type is available in Oracle Database 21c and later. A JSON column validates the document when Oracle stores it.
Choosing a column type
A native JSON column is usually the clearest choice for new applications on Oracle Database 21c or later. Older designs commonly use VARCHAR2, CLOB, or BLOB with a check constraint.
| Column definition | Typical use | Validation |
|---|---|---|
| `JSON` | New JSON-first designs | Built into the type |
| `VARCHAR2` with `IS JSON` | Small documents | Check constraint |
| `CLOB` with `IS JSON` | Large text documents | Check constraint |
| `BLOB` with `IS JSON` | Binary or controlled-encoding storage | Check constraint |
The choice affects storage and limits, but it does not change the SQL/JSON path language used to query the document. A constraint checks whether the value is valid JSON. It does not require every document to have the same fields or data types.
Creating a native JSON column
The following table stores an order identifier and a nested order document:
CREATE TABLE orders (
id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
doc JSON
);
INSERT INTO orders (doc) VALUES (
'{"orderId": 1001,
"customer": {"name": "Ada Lovelace", "city": "London"},
"status": "NEW",
"items": [
{"sku": "A-1", "qty": 2, "price": 9.5},
{"sku": "B-7", "qty": 1, "price": 24}
]}'
);The outer value is an object. Its customer field contains another object. Its items field contains an array of objects.
The identity column gives the database row a relational key. The JSON document gives the order its flexible document structure. Keeping both is useful: use the relational key for joins and references, and use JSON fields for document content that changes more often.
Validating older column types
If your database or application uses a text column, add an IS JSON check:
CREATE TABLE event_log (
event_id NUMBER PRIMARY KEY,
event_time TIMESTAMP NOT NULL,
payload CLOB CONSTRAINT event_payload_is_json CHECK (payload IS JSON)
);
INSERT INTO event_log (event_id, event_time, payload)
VALUES (
1,
SYSTIMESTAMP,
'{"type":"login","userId":42,"success":true}'
);An invalid value fails at insert or update time:
INSERT INTO event_log (event_id, event_time, payload)
VALUES (2, SYSTIMESTAMP, '{"type":"login",}');Use VARCHAR2 when documents have a known, small maximum size. Use CLOB for larger text documents. A BLOB can be appropriate when the producer controls the encoding and binary storage is useful. Validate at the database boundary rather than trusting every client.
Practical rules and common errors
- JSON field names are case-sensitive.
customer.nameandCustomer.Nameaddress different fields. - A JSON number is not automatically a relational
NUMBERin every query. Specify the desired return type when extracting values. - JSON
nulland SQLNULLare different concepts. A missing field can also behave differently from a field whose value is JSONnull. IS JSONvalidates syntax, not business rules. It will accept documents that omitorderIdunless you add separate checks or validate in application logic.- Do not store JSON as an unvalidated
VARCHAR2,CLOB, orBLOB. One malformed document can make later queries fail or produce unexpected results.
Exercise
Which definition is safer for a large text JSON payload: a plain CLOB or a CLOB with CHECK (payload IS JSON), and why?
Show answer
A CLOB with the IS JSON check is safer because Oracle rejects syntactically invalid JSON before storing it. The check does not enforce required fields or business rules.