tencent cloud

Cloud Log Service

JSON Functions

Download
포커스 모드
폰트 크기
마지막 업데이트 시간: 2026-06-04 18:17:48
This section describes the basic syntax and examples of JSON functions.
Function Name
Statement
Description
json_array_contains(x,value)
Determines whether a JSON array contains a specified value.
json_array_get(x,index)
Obtains the element at a specified index in a JSON array.
json_array_length(x)
Calculates the number of elements in a JSON array. If x is not a JSON array, null is returned.
json_extract(x,json_path)
Extracts a set of JSON values (array or object) from a JSON object or array.
json_extract_scalar(x,json_path)
Extracts a set of scalar values (strings, integers, or Boolean values) from a JSON object or array. This function is similar to the json_extract function.
json_format(x)
Converts a JSON value into a string value.
json_parse(x)
Converts a string value into a JSON value.
json_size(x,json_path)
Calculates the number of elements in a JSON array or object.

json_array_contains Function

The json_array_contains function is used to determine whether a JSON array contains a specified value.

Syntax

json_array_contains(x, value)

Parameter Description

Parameter
Description
x
The parameter value must be a JSON array.
value
Value.

Return Value Type

It returns values of the boolean type.

Example

Determine whether the JSON array [1, 2, 3] contains the value 2.
Query and analysis statements.
* | SELECT json_array_contains('[1, 2, 3]', 2)
Query and analysis results.
true

json_array_get Function

The json_array_get function is used to obtain the element at a specified index in a JSON array.

Syntax

json_array_get(x, index)

Parameter Description

Parameter
Description
x
The parameter value must be a JSON array.
index
JSON index, starting from 0.

Return Value Type

It returns values of the varchar type.

Example

Return the element at index 1 from the JSON array ["a", [3, 9], "c"].
Query and analysis statements.
* | SELECT json_array_get('["a", [3, 9], "c"]', 1)
Query and analysis results.
[3,9]

json_array_length Function

The json_array_length function is used to calculate the number of elements in a JSON array. If x is not a JSON array, null is returned.

Syntax

json_array_length(x)

Parameter Description

Parameter
Description
x
The parameter value must be a JSON array.

Return Value Type

It returns values of the bigint type.

Example

Example 1: Calculate the number of JSON elements in the apple.message field value.
apple.message:[{"traceName":"StoreMonitor"},{"topicName":"persistent://apache/pulsar/test-partition-17"},{"producerName":"pulsar-mini-338-36"},{"localAddr":"pulsar://pulsar-mini-broker-5.pulsar-mini-broker.pulsar.svc.cluster.local:6650"},{"sequenceId":826},{"storeTime":1635905306062},{"messageId":"19422-24519"},{"status":"SUCCESS"}]
Query and analysis statements.
* | SELECT json_array_length(apple.message)
Query and analysis results.
8

json_extract Function

The json_extract function is used to extract a set of JSON values (array or object) from a JSON object or array.

Syntax

json_extract(x, json_path)

Parameter Description

Parameter
Description
x
The parameter value must be a JSON object or array.
json_path
JSONPath-formatted JSON path, for example, $.store.book[0].title
Note:
JSON syntax that requires traversing array elements is not supported, such as $.store.book[*].author, $..book[(@.length-1)], $..book[?(@.price<10)], and so on.
When an element name contains special characters, use the [] syntax instead of the . syntax and enclose the element name in double quotation marks, for example, $.store["book name"].

Return Value Type

JSON string.

Example

Obtain the value of epochSecond in the apple.instant field.
Field examples.
apple.instant:{"epochSecond":1635905306,"nanoOfSecond":63001000}
Query and analysis statements.
* | SELECT json_extract(apple.instant, '$.epochSecond')
Query and analysis results.
1635905306

json_extract_scalar Function

The json_extract_scalar function is used to extract a set of scalar values (strings, integers, or Boolean values) from a JSON object or array.

Syntax

json_extract_scalar(x, json_path)

Parameter Description

Parameter
Description
x
The parameter value must be a JSON array.
json_path
JSONPath-formatted JSON path, for example, $.store.book[0].title
Note:
JSON syntax that requires traversing array elements is not supported, such as $.store.book[*].author, $..book[(@.length-1)], $..book[?(@.price<10)], and so on.
When an element name contains special characters, use the [] syntax instead of the . syntax and enclose the element name in double quotation marks, for example, $.store["book name"].

Return Value Type

It returns values of the varchar type.

Example

Obtain the value of the epochSecond field from the apple.instant field, convert the value into a bigint value, and calculate the sum.
Field examples.
apple.instant:{"epochSecond":1635905306,"nanoOfSecond":63001000}
Query and analysis statements.
* | SELECT sum(cast(json_extract_scalar(apple.instant,'$.epochSecond') AS bigint) )
Query and analysis results.
1635905306

json_format Function

The json_format function is used to convert a JSON value into a string value.

Syntax

json_format(x)

Parameter Description

Parameter
Description
x
The parameter value must be of the JSON type.

Return Value Type

It returns values of the varchar type.

Example

Convert the JSON array [1,2,3] into a string.
Query and analysis statements.
* | SELECT json_format(json_parse('[1, 2, 3]'))
Query and analysis results.
[1, 2, 3]

json_parse Function

The json_parse function is used to convert a string value into a JSON value and determine whether it conforms to a valid JSON format.

Syntax

json_parse(x)

Parameter Description

Parameter
Description
x
The parameter value must be a string.

Return Value Type

JSON type.

Example

Convert the string [1, 2, 3] into a JSON array.
Query and analysis statements.
* | SELECT json_parse('[1, 2, 3]')
Query and analysis results.
[1, 2, 3]

json_size Function

The json_size function is used to calculate the number of elements in a JSON object or array.

Syntax

json_size(x, json_path)

Parameter Description

Parameter
Description
x
The parameter value must be a JSON object or array.
json_path
JSONPath-formatted JSON path, for example, $.store.book[0].title
Note:
JSON syntax that requires traversing array elements is not supported, such as $.store.book[*].author, $..book[(@.length-1)], $..book[?(@.price<10)], and so on.
When an element name contains special characters, use the [] syntax instead of the . syntax and enclose the element name in double quotation marks, for example, $.store["book name"].

Return Value Type

It returns values of the bigint type.

Example

Obtain the length of the JSON array [1, 2, 3].
Query and analysis statements.
* | SELECT json_size(json_parse('[1, 2, 3]'),'$')
Query and analysis results.
3


도움말 및 지원

문제 해결에 도움이 되었나요?

피드백