Skip to content

Commit 0a4ad41

Browse files
committed
Show JSON examples with 12.2's easier dot notation
1 parent 479a1a8 commit 0a4ad41

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

doc/api.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5570,17 +5570,17 @@ As an example, the following table has a `PO_DOCUMENT` column that is
55705570
enforced to be JSON:
55715571
55725572
```sql
5573-
CREATE TABLE po (po_document VARCHAR2(4000) CHECK (po_document IS JSON));
5573+
CREATE TABLE j_purchaseorder (po_document VARCHAR2(4000) CHECK (po_document IS JSON));
55745574
```
55755575
55765576
To insert data using node-oracledb:
55775577
55785578
```javascript
5579-
var data = { customerId: 100, item: 1234, quantity: 2 };
5579+
var data = { "userId": 1, "userName": "Chris", "location": "Australia" };
55805580
var s = JSON.stringify(data); // change JavaScript value to a JSON string
55815581

55825582
connection.execute(
5583-
"INSERT INTO po (po_document) VALUES (:bv)",
5583+
"INSERT INTO j_purchaseorder (po_document) VALUES (:bv)",
55845584
[s] // bind the JSON string
55855585
function (err) {
55865586
. . .
@@ -5594,17 +5594,24 @@ and array ranges. An example is `$.friends` which is the value of
55945594
JSON field `friends`.
55955595
55965596
Oracle provides SQL functions and conditions to create, query, and
5597-
operate on JSON data stored in the database. An example is the Oracle
5598-
SQL Function `JSON_TABLE` which projects JSON data to a relational
5599-
format effectively making it usable like an inline relational view.
5600-
Another example is `JSON_EXISTS` which tests for the existence of a
5601-
particular value within some JSON data:
5597+
operate on JSON data stored in the database.
56025598
5603-
This example looks for JSON entries that have a `quantity` field:
5599+
For example, `j_purchaseorder` can be queried with:
5600+
5601+
```
5602+
"SELECT po.po_document.location FROM j_purchaseorder po"
5603+
```
5604+
5605+
With the earlier JSON inserted into the table, the queried value would
5606+
be `Australia`.
5607+
5608+
The `JSON_EXISTS` tests for the existence of a particular value within
5609+
some JSON data. To look for JSON entries that have a `quantity`
5610+
field:
56045611
56055612
```JavaScript
56065613
conn.execute(
5607-
"SELECT po_document FROM po WHERE JSON_EXISTS (po_document, '$.quantity')",
5614+
"SELECT po_document FROM j_purchaseorder WHERE JSON_EXISTS (po_document, '$.location')",
56085615
function(err, result) {
56095616
if (err) {
56105617
. . .
@@ -5615,10 +5622,10 @@ conn.execute(
56155622
});
56165623
```
56175624
5618-
After the previous `INSERT` example, this query would display:
5625+
This query would display:
56195626
56205627
```
5621-
{ customerId: 100, item: 1234, quantity: 2 }
5628+
{ userId: 1, userName: 'Chris', location: 'Australia' }
56225629
```
56235630
56245631
In Oracle Database 12.2 the [`JSON_OBJECT` ][54] function is a great

examples/selectjson.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,24 @@ var dojsonquery = function (conn, cb) {
8888
});
8989
};
9090

91-
// 2. Using JSON_VALUE to extract a value from a JSON column
91+
// 2. Extract a value from a JSON column. This syntax requires Oracle Database 12.2
92+
var dorelationalquerydot = function (conn, cb) {
93+
console.log('2. Using dot-notation to extract a value from a JSON column');
94+
conn.execute(
95+
"SELECT po.po_document.location FROM j_purchaseorder po",
96+
function(err, result) {
97+
if (err) {
98+
return cb(err, conn);
99+
} else {
100+
console.log('Query results: ', result.rows[0][0]); // just show first record
101+
return cb(null, conn);
102+
}
103+
});
104+
};
105+
106+
// 3. Using JSON_VALUE to extract a value from a JSON column
92107
var dorelationalquery = function (conn, cb) {
93-
console.log('2. Using JSON_VALUE to extract a value from a JSON column');
108+
console.log('3. Using JSON_VALUE to extract a value from a JSON column');
94109
conn.execute(
95110
"SELECT JSON_VALUE(po_document, '$.location') FROM j_purchaseorder",
96111
function(err, result) {
@@ -103,9 +118,9 @@ var dorelationalquery = function (conn, cb) {
103118
});
104119
};
105120

106-
// 3. Using JSON_OBJECT to extract relational data as JSON
121+
// 4. Using JSON_OBJECT to extract relational data as JSON
107122
var dojsonfromrelational = function (conn, cb) {
108-
console.log('3. Using JSON_OBJECT to extract relational data as JSON');
123+
console.log('4. Using JSON_OBJECT to extract relational data as JSON');
109124
if (conn.oracleServerVersion < 1202000000) { // JSON_OBJECT is new in Oracle Database 12.2
110125
console.log('The JSON_OBJECT example only works with Oracle Database 12.2 or greater');
111126
return cb(null, conn);
@@ -133,6 +148,7 @@ async.waterfall(
133148
checkver,
134149
doinsert,
135150
dojsonquery,
151+
dorelationalquerydot,
136152
dorelationalquery,
137153
dojsonfromrelational
138154
],

examples/selectjsonblob.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* selectjsonblob.js
2020
*
2121
* DESCRIPTION
22-
* Executes sample insert and query using a JSON column with BLOB storage.
22+
* Executes sample insert and query statements using a JSON column with BLOB storage.
2323
* Requires Oracle Database 12.1.0.2, which has extensive JSON datatype support.
2424
* See https://docs.oracle.com/database/122/ADJSN/toc.htm
2525
*
@@ -70,10 +70,10 @@ var doinsert = function (conn, cb) {
7070
});
7171
};
7272

73-
// Select JSON stored in a BLOB column
73+
// Select JSON with JSON_EXISTS
7474

7575
var dojsonquery = function (conn, cb) {
76-
console.log('Selecting JSON stored in a BLOB column');
76+
console.log('Selecting JSON stored in a BLOB column:');
7777
conn.execute(
7878
"SELECT po_document FROM j_purchaseorder_b WHERE JSON_EXISTS (po_document, '$.location')",
7979
[],
@@ -84,18 +84,35 @@ var dojsonquery = function (conn, cb) {
8484
if (result.rows.length === 0)
8585
return cb(new Error('No results'), conn);
8686

87-
console.log('Query results:');
8887
console.log(result.rows[0][0].toString('utf8'));
8988
return cb(null, conn);
9089
});
9190
};
9291

92+
// Select a JSON value using dot-notation. This syntax requires Oracle Database 12.2
93+
94+
var dojsonquerydot = function (conn, cb) {
95+
console.log('Selecting a JSON value:');
96+
conn.execute(
97+
"SELECT pob.po_document.location FROM j_purchaseorder_b pob",
98+
function(err, result) {
99+
if (err)
100+
return cb(err, conn);
101+
if (result.rows.length === 0)
102+
return cb(new Error('No results'), conn);
103+
104+
console.log(result.rows[0][0]);
105+
return cb(null, conn);
106+
});
107+
};
108+
93109
async.waterfall(
94110
[
95111
doconnect,
96112
checkver,
97113
doinsert,
98-
dojsonquery
114+
dojsonquery,
115+
dojsonquerydot
99116
],
100117
function (err, conn) {
101118
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }

0 commit comments

Comments
 (0)