binary strin gs like BLOB) to carry JSON values. Since
there is no standard JSON query language yet, the SQL
standard defines a path language for navigation within
JSON data and a set of SQL built-in functions and
predicates for querying within a JSON document.
We will ill ustrate the SQL/JSON
5
path language and
SQL/JSON operators in the following sections. Due to
space restrictions, we cannot cover all SQ L/JSON
features. For a detailed description of the SQL/JSON
functionality the interested reade r is referred to [15].
2.1 Querying JSON in SQL
2.1.1 Sample data
Our examples will use the table T shown below:
ID JCOL
111 { "Name" : "John Smith",
"address" : {
"streetAddress": "21 2nd Street",
"city": "New York",
"state" : "NY",
"postalCode" : 10021 },
"phoneNumber" : [
{ "type" : "home",
"number" : "212 555-1234" },
{ "type" : "fax",
"number" : "646 555-4567" } ] }
222 { "Name" : "Peter Walker",
"address" : {
"streetAddress": "111 Main Street",
"city": "San Jose",
"state" : "CA",
"postalCode" : 95111 },
"phoneNumber" : [
{ "type" : "home",
"number" : "408 555-9876" },
{ "type" : "office",
"number" : "650 555-2468" } ] }
333 { "Name" : "James Lee" }
In T, the column JCOL contains JSON data stored in a
character string.
Curly braces { } enclose J SON objects. A JSON
object has zero or more comma-separated key/value
pairs, called members. The key is a character string
before a colon; the value is a JSON value placed after a
colon. For example, in each row, the outermost JSON
object has a key called "Name" with varying values in
each row.
5
The support for JSON is specified in Foundation [2] and
not in a separate p art (as is done, e.g., for SQL/XML [9]).
Still, the moniker SQL/JSON is associated with the JSON-
specific functionality in SQL.
Square brackets [ ] enclose JSON arrays. A JSON
array is an ordered, comma-separated list of JSON
values. In the first and second rows, the key called
"phoneNumber" has a value which is a JSON array.
This illustrates how JSON objects and arrays can be
nested arbitrarily.
Scalar JSON values are character strings, numbers, and
the literals true, false and null.
The sample data is fairly homogeneous, but this is not
a requirement of JSON. For example, the elements of
an array do not need to be of the same type, and
objects in different r ows do not have to have the same
keys.
2.1.2 IS J SON predicate
The IS JSON predicate is used to verify that an SQL
value contains a syntactically correct JSON value. For
example, this predicate can be used in a column check
constraint, like this:
CREATE TABLE T (
Id INTEGER PRIMARY KEY,
Jcol CHARACTER VARYING ( 5000 )
CHECK ( Jcol IS JSON ) )
The preceding might have been used to create the table
T, insuring that the value of Jcol is valid JSON in all
rows of T .
In the absence of such a constraint, one could use IS
JSON as a filter to locate valid JSON data, like this:
SELECT * FROM T WHERE Jcol IS JSON
2.1.3 SQL/JSON path expressions
The r emaining SQL/JSON operators to query JSON
use the SQL/JSON path la nguage. It is used to
navigate within a JSON value to its components. It is
similar to XPath for XML and also somewhat si milar
to object/array navigation in the JavaScript language.
A path expression is composed of a sequence of path
steps; each step can be associated with a set of
predicates.
2.1.4 JSON_EXISTS predicate
JSON_EXISTS is used to determine if an SQL/JSON
path expression has any matches in a JSON document.
For example, this query finds the IDs of the rows with
a key called “address”:
SELECT Id
FROM T
WHERE JSON_EXISTS ( Jcol,
'strict $.address' )
The example works as follows. The first argument to
JSON_EXISTS, Jcol, sp ecifies the context item
(JSON value) on which JSON_EXISTS operates. The
keyword strict selects the strict mode; the
52 SIGMOD Record, June 2018 (Vol. 47, No. 2)