24.10 Multiple Data Guides Per Document Set
A data guide reflects the shape of a given set of JSON documents. If a JSON column contains different types of documents, with different structure or type information, you can create and use different data guides for the different kinds of documents.
Data Guides For Different Kinds of JSON Documents
JSON documents need not, and typically do not, follow a prescribed schema. This is true even for documents that are used similarly in a given application; they may differ in structural ways (shape), and field types may differ.
A JSON data guide summarizes the structural and type information of a given set of documents. In general, the more similar the structure and type information of the documents in a given set, the more useful the resulting data guide.
A data guide is created for a given column of JSON data. If the column contains very different kinds of documents (for example, purchase orders and health records) then a single data guide for the column is likely to be of limited use.
One way to address this concern is to put different kinds of JSON documents in different JSON columns. But sometimes other considerations decide in favor of mixing document types in the same column.
In addition, documents of the same general type, which you decide to store in the same column, can nevertheless differ in relatively systematic ways. This includes the case of evolving document shape and type information. For example, the structure of tax-information documents could change from year to year.
When you create a data guide you can decide which information to summarize. And you can thus create different data guides for the same JSON column, to represent different subsets of the document set.
An additional aid in this regard is to have a separate, non-JSON, column in the same table, which is used to label, or categorize, the documents in a JSON column.
In the case of the purchase-order documents
used in our examples, let’s suppose that their structure can evolve significantly from year
to year, so that column date_loaded
of table
j_purchaseorder
can be used to group them into subsets of reasonably
similar shape. Example 24-17 adds a purchase-order document for 2015, and Example 24-18 adds a purchase-order document for 2016. (Compare with the
documents for 2014, which are added in Example 4-3.)
Using a SQL Aggregate Function to Create Multiple Data Guides
Oracle SQL
function json_dataguide
is in fact an aggregate function. An
aggregate function returns a single result row based on groups of rows, rather than on a
single row. It is typically used in a SELECT
list for a query that has a
GROUP BY
clause, which divides the rows of a queried table or view into
groups. The aggregate function applies to each group of rows, returning a single result row
for each group. For example, aggregate function avg
returns the average of
a group of values.
Function json_dataguide
aggregates
JSON data to produce a summary, or specification, of it, which is returned in the form of a
JSON document. In other words, for each group of JSON documents to which they are applied,
they return a data guide.
If you omit GROUP BY
then this
function returns a single data guide that summarizes all of the JSON data in the subject
JSON column.
Example 24-19 queries the documents of JSON column data
,
grouping them to produce three data guides, one for each year of column
date_loaded
.
Example 24-17 Adding a 2015 Purchase-Order Document
The 2015 purchase-order format uses only part number,
reference, and line-items as its top-level fields, and these fields use prefix
PO_
. Each line item contains only a part number and a
quantity.
INSERT INTO j_purchaseorder
VALUES (
SYS_GUID(),
to_date('30-JUN-2015'),
'{"PO_Number" : 4230,
"PO_Reference" : "JDEER-20140421",
"PO_LineItems" : [ {"Part_Number" : 230912362345,
"Quantity" : 3.0} ]}');
Example 24-18 Adding a 2016 Purchase-Order Document
The 2016 format uses
PO_ID
instead of PO_Number
, PO_Ref
instead of PO_Reference
, PO_Items
instead of
PO_LineItems
, Part_No
instead of
Part_Number
, and Item_Quantity
instead of
Quantity
.
INSERT INTO j_purchaseorder
VALUES (
SYS_GUID(),
to_date('30-MAR-2016'),
'{"PO_ID" : 4230,
"PO_Ref" : "JDEER-20140421",
"PO_Items" : [ {"Part_No" : 98981327234,
"Item_Quantity" : 13} ]}');
Example 24-19 Creating Multiple Data Guides With Aggregate Function JSON_DATAGUIDE
This example uses aggregate SQL function json_dataguide
to obtain
three flatFoot 1 data guides, one for
each year-specific format. The data guide for 2014 is shown only partially — it is the same
as the data guide from A Flat Data Guide For Purchase-Order Documents, except
that no statistics fields are present. (Data guides returned by functions
json_dataguide
do not contain any statistics fields.
SELECT extract(YEAR FROM date_loaded), json_dataguide(data)
FROM j_purchaseorder
GROUP BY extract(YEAR FROM date_loaded)
ORDER BY extract(YEAR FROM date_loaded) DESC;
EXTRACT(YEARFROMDATE_LOADED)
----------------------------
JSON_DATAGUIDE(DATA)
--------------------
2016
[
{
"o:path" : "$.PO_ID",
"type" : "number",
"o:length" : 4
},
{
"o:path" : "$.PO_Ref",
"type" : "string",
"o:length" : 16
},
{
"o:path" : "$.PO_Items",
"type" : "array",
"o:length" : 64
},
{
"o:path" : "$.PO_Items.Part_No",
"type" : "number",
"o:length" : 16
},
{
"o:path" : "$.PO_Items.Item_Quantity",
"type" : "number",
"o:length" : 2
}
]
2015
[
{
"o:path" : "$.PO_Number",
"type" : "number",
"o:length" : 4
},
{
"o:path" : "$.PO_LineItems",
"type" : "array",
"o:length" : 64
},
{
"o:path" : "$.PO_LineItems.Quantity",
"type" : "number",
"o:length" : 4
},
{
"o:path" : "$.PO_LineItems.Part_Number",
"type" : "number",
"o:length" : 16
},
{
"o:path" : "$.PO_Reference",
"type" : "string",
"o:length" : 16
}
]
2014
[
{
"o:path" : "$.User",
"type" : "string",
"o:length" : 8
},
{
"o:path" : "$.PONumber",
"type" : "number",
"o:length" : 4
},
...
{
"o:path" : "$.\"Special Instructions\"",
"type" : "string",
"o:length" : 8
}
]
3 rows selected.
See Also:
Oracle Database SQL
Language Reference for information about SQL function
json_dataguide
Parent topic: JSON Data Guide
Footnote Legend
Footnote 1: If function json_dataguide were passedDBMS_JSON.FORMAT_HIERARCHICAL
or
DBMS_JSON.FORMAT_SCHEMA
as optional second argument, then the result
would be three hierarchical or schema data guides, respectively.