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 columnj 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 INTOto write data into a JSON-type column (e.g., columnj).
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
jsonpathsto$. - To import and store the value of a JSON object in a JSON file as JSON type, set
jsonpathsto$.a(wherearepresents the key). For more JSON path expressions, refer to JSON path.
- To import and store the JSON object at the root node of a JSON file as JSON type, set
- Method 3: Use Broker Load to import Parquet files and store them as JSON type. For import methods, please refer to Broker Load.
- 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 tabletj for illustration.
id=1.
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.
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 fieldevent_data in the events table, with the following content:
- Extract array elements: Extract specific fields such as type, timestamp, etc., from the actions array and perform projection operations.
- Array expansion: Use the
json_eachfunction to expand the nested JSON array into a multi-row, multi-column table structure for subsequent analysis. - 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 thejson_each function to expand JSON arrays, converting them into multiple rows of data. For example:
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: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.