Bringing JSON To The Table

As a long tine use of Relational Databases I live by the rule that data must be stored in and retrieved from table. And those tables must have columns. But JSON plays by it’s own rules by allowing random attribute-value pairs with no consistency enforced. But the latest version of my most used database, Oracle offers a way to store and retrieve this data in a JSON column in a table; and to retrieve it with a JSON_TABLE function. Here is an example:

-- create a table with a single JSON column
SQL> CREATE TABLE its_json_thanks
 2 ( json_only_please CLOB,
 3 CONSTRAINT json_only
 4 CHECK ( json_only_please IS JSON ) );

Table created.

-- Insert some valid JSON
SQL> INSERT INTO its_json_thanks
 3 '
 4 {
 5 "primary_key" : "1",
 6 "unique_key" : "1"
 7 }');

1 row created.

-- Query the values out
 2 uk
 3 FROM its_json_thanks ijt,
 4 JSON_TABLE(json_only_please,'$'
 5 COLUMNS( pk VARCHAR2(10) PATH '$.primary_key',
 6 uk VARCHAr2(10) PATH '$.unique_key' ))
 7 WHERE ijt.json_only_please.primary_key = '1'
 8 /

---------- ---------- 
1 1

1 row selected

The JSON_TABLE function performs a self-join between the table and the JSON column it contains. It also aligns the attributes (primary key and unique key) with column names (PK and UK).

Oracle quite adept at serving non-tabular data (XML,etc) up in a columnar format for applications that need to see it that way while allowing other applications to interact in their native format.

I’d explain the JSON_TABLE example as a conversation between a maitre’d and a customer like this:

MD: Welcome, Primary JSON. How many in your party?

MRJ: Two please. Me and my wife, Unique.

MD: Ah yes, the famous PK and UK. We have a one dollar,
    two seater near the window for you.

Or maybe not.

Thanks for reading.