Skip to main content
Since version 2.2.0, the database supports JSON. This article introduces the basic concepts of JSON, and how the database creates JSON-type columns, loads, and queries JSON data, and constructs and processes JSON data through JSON functions and operators.

What is JSON

JSON is a lightweight data interchange format. JSON-type data is semi-structured and supports a tree structure. JSON data is hierarchical, flexible, easy to read and process, and is widely used in data storage and analysis scenarios. JSON supports data types such as NUMBER, STRING, BOOLEAN, ARRAY, OBJECT, and NULL values. For more information on JSON, please refer to the JSON official website. For JSON data input and output syntax, please refer to the JSON specification RFC 7159. The database supports storing and efficiently querying and analyzing JSON data. The database uses binary format encoding to store JSON data instead of storing the input text directly, which reduces parsing costs during data computation and queries, thereby improving query efficiency.

Using JSON Data

Creating JSON-Type Columns

When creating a table, specify the column j as JSON type using the keyword JSON.

Loading Data and Storing as JSON Type

The database supports the following methods to load data and store it as JSON type.
  • Method 1: Use INSERT INTO to write data into a JSON-type column (e.g., column j).
The PARSE_JSON function can construct JSON-type data based on string-type data. The JSON_OBJECT function can construct JSON object-type data, allowing existing tables to be converted to JSON type. For more information, please refer to PARSE_JSON and JSON_OBJECT.
  • Method 2: Use Stream Load to import JSON files and store them as JSON type. For import methods, please refer to Import JSON Data.
    • To import and store the JSON object at the root node of a JSON file as JSON type, set jsonpaths to $.
    • To import and store the value of a JSON object in a JSON file as JSON type, set jsonpaths to $.a (where a represents the key). For more JSON path expressions, refer to JSON path.
  • Method 3: Use Broker Load to import Parquet files and store them as JSON type. For import methods, please refer to Broker Load.
Data type conversion is supported during import as follows:
  • Method 4: Use Routine Load to continuously consume JSON format data from Kafka and import it into the database.

Querying and Processing JSON-Type Data

The database supports querying and processing JSON-type data and supports using JSON functions and operators. This example uses the table tj for illustration.
Example 1: Filter data in the JSON-type column that meets the condition id=1.
Example 2: Filter data in the table based on the JSON-type column.
In the following example, j->'a' returns JSON-type data. You can compare it with the first example, which performs implicit conversion on the data; or use the CAST function to construct JSON-type data as INT for comparison.
Example 3: Filter data in the table based on the JSON-type column (you can use the CAST function to construct the JSON-type column as BOOLEAN type).
Example 4: Filter data in the JSON-type column that meets the condition and perform numerical operations.
Example 5: Sort based on the JSON-type column.

JSON Functions and Operators

JSON functions and operators can be used to construct and process JSON data. For detailed information, please refer to JSON Functions and Operators.

JSON Array

JSON can contain nested data, such as Objects, Arrays, or other JSON data types nested within an Array. The database provides a rich set of functions and operators to handle these complex nested JSON data structures. The following will introduce how to handle JSON data containing arrays. Suppose there is a JSON field event_data in the events table, with the following content:
The following examples demonstrate several common JSON array analysis scenarios:
  1. Extract array elements: Extract specific fields such as type, timestamp, etc., from the actions array and perform projection operations.
  2. Array expansion: Use the json_each function to expand the nested JSON array into a multi-row, multi-column table structure for subsequent analysis.
  3. Array computation: Use Array Functions to filter, transform, and aggregate array elements, such as counting the number of specific types of operations.

1. Extract Elements from JSON Array

To extract a nested element from a JSON Array, you can use the following syntax:
  • The return type is still a JSON Array, and you can use the CAST expression for type conversion.

2. Expand Using json_each

The database provides the json_each function to expand JSON arrays, converting them into multiple rows of data. For example:
To extract the type and timestamp fields separately:
After this, the JSON Array data becomes a familiar relational model, allowing the use of common functions for analysis.

3. Use Array Functions for Filtering and Calculation

The database also supports ARRAY-related functions, which can be used in conjunction with JSON functions for more efficient queries. By combining these functions, you can filter, transform, and aggregate JSON array data. The following example demonstrates how to use these functions:
Furthermore, you can combine other ARRAY Functions to perform aggregation calculations on array elements:

Limitations and Considerations

  • The maximum supported length for JSON-type data is currently 16 MB.
  • ORDER BY, GROUP BY, and JOIN clauses do not support referencing JSON-type columns. If you need to reference them, you can use the CAST function in advance to convert JSON-type columns to other SQL types. For specific conversion methods, please refer to JSON Type Conversion.
  • JSON-type columns can exist in Duplicate Key tables, Primary Key tables, and Unique Key tables, but not in Aggregate tables.
  • JSON-type columns are not supported as partition keys, bucketing keys, or dimension columns in Duplicate Key tables, Primary Key tables, and Unique Key tables, and cannot be used in JOIN, GROUP BY, or ORDER BY clauses.
  • The database supports using <, <=, >, >=, =, != operators to query JSON data but does not support the IN operator.