@@ -15,52 +15,29 @@ For more information about using JSON in Oracle Database see the
15
15
`Database JSON Developer’s Guide <https://www.oracle.com/pls/topic/
16
16
lookup?ctx=dblatest&id=ADJSN> `__.
17
17
18
- **Oracle Database 12c JSON Data Type **
19
-
20
- Prior to Oracle Database 21, JSON in relational tables is stored as
21
- BLOB, CLOB or VARCHAR2 data, allowing easy access with node-oracledb.
22
- All of these types can be used with node-oracledb in Thin or Thick mode.
23
-
24
- The older syntax to create a table with a JSON column is like:
25
-
26
- .. code-block :: sql
27
-
28
- CREATE TABLE j_purchaseorder (po_document BLOB CHECK (po_document IS JSON));
29
-
30
- The check constraint with the clause ``IS JSON `` ensures only JSON data
31
- is stored in that column.
32
-
33
- The older syntax can still be used in Oracle Database 21, however the
34
- recommendation is to move to the new JSON type. With the old syntax, the
35
- storage can be BLOB, CLOB, or VARCHAR2. Of these, BLOB is preferred to
36
- avoid character set conversion overheads.
18
+ .. _json21ctype :
37
19
38
- **Oracle Database 21c JSON Data Type **
20
+ Using the Oracle Database 21c JSON Type in node-oracledb
21
+ ========================================================
39
22
40
23
Oracle Database 21c introduced a dedicated JSON data type with a new
41
24
`binary storage format <https://blogs.oracle.com/database/post/
42
25
autonomous-json-database-under-the-covers-oson-format> `__
43
- that improves performance and functionality. To use the new dedicated
44
- JSON type, you can use node-oracledb 5.1 or later. The 21c JSON data
45
- type can be used in both node-oracledb Thin and Thick modes. With Thick mode,
46
- the Oracle Client libraries must be version 21, or later.
26
+ that improves performance and functionality. To take advantage of the new
27
+ dedicated JSON type in Oracle Database 21c and later versions, use
28
+ node-oracledb 5.1 or later. For Thick mode, you must additionally use
29
+ Oracle Client 21c ( or later) .
47
30
48
- In Oracle Database 21 or later, to create a table with a column called
31
+ In Oracle Database 21c or later, to create a table with a column called
49
32
``PO_DOCUMENT `` for JSON data:
50
33
51
34
.. code-block :: sql
52
35
53
36
CREATE TABLE j_purchaseorder (po_document JSON);
54
37
55
- .. _ json21ctype :
38
+ ** Inserting JSON Data **
56
39
57
- Using the Oracle Database 21c JSON Type in node-oracledb
58
- ========================================================
59
-
60
- Using node-oracledb Thin mode with Oracle Database 21c or later, or using
61
- node-oracledb Thick mode or node-oracledb 5.1 (or later) with Oracle Database
62
- 21c (or later) and Oracle Client 21c (or later), you can insert JavaScript
63
- objects directly by binding as ``oracledb.DB_TYPE_JSON ``:
40
+ To insert JavaScript objects directly by binding as ``oracledb.DB_TYPE_JSON ``:
64
41
65
42
.. code-block :: javascript
66
43
@@ -71,6 +48,10 @@ objects directly by binding as ``oracledb.DB_TYPE_JSON``:
71
48
{ bv: {val: data, type: oracledb .DB_TYPE_JSON } }
72
49
);
73
50
51
+ .. _json21fetch :
52
+
53
+ **Fetching JSON Data **
54
+
74
55
To query a JSON column, use:
75
56
76
57
.. code-block :: javascript
@@ -86,11 +67,11 @@ The output is::
86
67
}
87
68
]
88
69
89
- Using Oracle Client Libraries 19 or Earlier
90
- -------------------------------------------
70
+ Using Oracle Client Libraries 19c or Earlier
71
+ --------------------------------------------
91
72
92
- If node-oracledb Thick mode uses Oracle Client Libraries 19 (or earlier),
93
- querying an Oracle Database 21 (or later), then JSON column returns a
73
+ If node-oracledb Thick mode uses Oracle Client Libraries 19c (or earlier),
74
+ querying an Oracle Database 21c (or later), then JSON column returns a
94
75
:ref: `Lob Class <lobclass >` BLOB. You can stream the Lob or use
95
76
:meth: `lob.getData() `:
96
77
@@ -117,6 +98,26 @@ shown above.
117
98
Using the Oracle Database 12c JSON Type in node-oracledb
118
99
========================================================
119
100
101
+ In Oracle Database versions 12c or later (prior to Oracle Database 21c), JSON
102
+ in relational tables is stored as BLOB, CLOB, or VARCHAR2 data. All of these
103
+ types can be used with node-oracledb in Thin or Thick mode.
104
+
105
+ The older syntax to create a table with a JSON column is like:
106
+
107
+ .. code-block :: sql
108
+
109
+ CREATE TABLE j_purchaseorder (po_document BLOB CHECK (po_document IS JSON));
110
+
111
+ The check constraint with the clause ``IS JSON `` ensures only JSON data
112
+ is stored in that column.
113
+
114
+ The older syntax can still be used in Oracle Database 21c. However the
115
+ recommendation is to move to the new JSON type. With the old syntax, the
116
+ storage can be BLOB, CLOB, or VARCHAR2. Of these, BLOB is preferred to
117
+ avoid character set conversion overheads.
118
+
119
+ **Inserting JSON Data **
120
+
120
121
When using Oracle Database 12c or later with JSON using BLOB storage, you can
121
122
insert JSON strings:
122
123
@@ -131,11 +132,27 @@ insert JSON strings:
131
132
[b] // bind the JSON string
132
133
);
133
134
135
+ **Fetching JSON Data **
136
+
137
+ With Oracle Database 12c (or later), you can fetch VARCHAR2 and LOB columns
138
+ that contain JSON data in the same way that
139
+ :ref: `JSON type columns <json21fetch >` are fetched when using Oracle
140
+ Database 21c (or later). This can be done by setting
141
+ :attr: `oracledb.future.oldJsonColumnAsObj ` to the value *true * as shown below.
142
+ If you are using node-oracledb Thick mode, you must use Oracle Client 19c
143
+ (or later) for this setting to work. For example:
144
+
145
+ .. code-block :: javascript
146
+
147
+ oracledb .future .oldJsonColumnAsObj = true ;
148
+ const r = await conn .execute (` SELECT po_document FROM j_purchaseorder` );
149
+ console .dir (r .rows , { depth: null });
150
+
134
151
IN Bind Type Mapping
135
152
====================
136
153
137
154
When binding a JavaScript object as ``oracledb.DB_TYPE_JSON `` for
138
- ``oracledb.BIND_IN `` or ``oracledb.BIND_INOUT `` in Oracle Database 21
155
+ ``oracledb.BIND_IN `` or ``oracledb.BIND_INOUT `` in Oracle Database 21c
139
156
(or later), JavaScript values are converted to JSON attributes as shown
140
157
in the following table. The ‘SQL Equivalent’ syntax can be used in SQL
141
158
INSERT and UPDATE statements if specific attribute types are needed but
@@ -221,7 +238,7 @@ might be like::
221
238
Query and OUT Bind Type Mapping
222
239
===============================
223
240
224
- When getting Oracle Database 21 or later JSON values from the database, the
241
+ When getting Oracle Database 21c or later JSON values from the database, the
225
242
following attribute mapping occurs:
226
243
227
244
.. list-table-with-summary ::
0 commit comments