Working with JSON Data in SQL
JSON is a semi-structured data type used for holding JSON (JavaScript Object Notation) data. In Hazelcast, the SQL service supports the following ways of working with JSON data:
-
json
: Maps JSON data to a single column ofJSON
type where you can use JsonPath syntax to query and filter it, including nested levels. -
json-flat
: Maps JSON top-level fields to columns with non-JSON types where you can query only top-level keys.
json
The JSON
data type comes with full support for querying JSON in maps and Kafka topics.
The JSON data type is currently not supported for file mappings. Files support the json-flat data type.
|
Mapping to the json
Type
You can map JSON data to a single column, by creating a mapping to one of the following data sources:
For example, to map the JSON data type to a value in a map:
CREATE MAPPING countries
TYPE IMap OPTIONS('keyFormat'='int', 'valueFormat'='json');
This statement creates a mapping to a map with two columns where the key is an INTEGER
and the value is JSON
:
-
__key
: A key as an integer -
this
: JSON data
Creating JSON Data
You can create new JSON data using a JSON-formatted string, or using one of the following JSON functions:
'{"country": "United Kingdom", "cities": [{"city": "London"}]}'
INSERT INTO countries VALUES (1, JSON_OBJECT('country':'United Kingdom', 'cities': JSON_ARRAY(JSON_OBJECT('city':'London'))))
Querying JSON
To query and/or filter JSON, use the JsonPath syntax in the following JSON functions:
The JSON_QUERY
function returns a JSON value, whereas the JSON_VALUE
function returns a non-JSON value, which is a VARCHAR
by default. If you use these functions to extract a string field, JSON_VALUE
will return the value directly, while JSON_QUERY
will return it as JSON, in single quotes, and with special characters escaped. JSON_VALUE
can’t be used to extract arrays or objects.
JsonPath is a query language for JSON. The JsonPath format starts with a dollar ($
) operator that refers to the root level of the JSON. From there, you can add other operators to traverse the data and filter the results.
For example, consider the following JSON data:
{
"countries": [
{
"country":"United Kingdom",
"cities": [{"city": "London"}]
},
{
"country":"France",
"cities": [{"city": "Lyon"}]
},
{
"country":"Turkey",
"cities": [{"city": "Istanbul"}]
}
]
}
The following examples show you how to use JsonPath to query this document.
JsonPath | Result |
---|---|
|
Returns the whole document.
|
|
Returns the
|
|
Returns all fields of the
|
|
Returns the
|
|
Returns the third element of the
|
|
Returns the first and third elements of the
|
|
Returns elements one through three of the
|
If the JsonPath does not match a value, NULL
is returned.
Supported JsonPath Operators
These are the supported operators from the JsonPath standard.
Operator | Description |
---|---|
|
Root |
|
Wildcard |
|
Child property |
|
Child property at any level deeper |
|
List of child properties |
|
List of array indexes |
|
Array range |
|
Filter expression |
|
Current node in filter expression |
json-flat
The json-flat
format comes with partial support for querying JSON in maps, Kafka topics, and files.
Mapping to the json-flat
Type
You can map JSON data to a multiple columns, by creating a mapping to one of the following data sources:
For example, consider the following JSON data:
{
"countries": "United Kingdom",
"cities": "London"
}
To map the JSON data using the json-flat
format in a map, you need to provide the JSON keys as column names:
CREATE MAPPING cities (
__key INT,
countries VARCHAR,
cities VARCHAR)
type IMap OPTIONS('keyFormat'='int', 'valueFormat'='json-flat');
This statement creates a mapping to a map that expects three columns:
-
__key
: A key as an integer -
countries
: A string -
cities
: A string
INSERT INTO cities VALUES
(1, 'United Kingdom','London');
Querying json-flat
To query the JSON values, select the column names.
SELECT cities AS City, countries AS Country
FROM cities;
+--------------------+--------------------+
|City |Country |
+--------------------+--------------------+
|London |United Kingdom |
+--------------------+--------------------+
JSON Type Conversion
SQL integer and floating-point types are converted into JSON
numbers. The DECIMAL
type and all temporal types are converted
into JSON strings.
JSON type | SQL Type |
---|---|
|
|
|
|
|
|