simpleJSON functions
Parse well-known JSON extremely fast.
Designed for parsing a limited subset of JSON extremely fast. These functions make strong assumptions about the JSON structure to optimize performance.
ClickHouse function reference
simpleJSONHas
Checks whether a field with the specified name exists in a JSON string.
Syntax
Arguments
json
(String
): JSON string to parse.field_name
(String
): Name of the field to check for.
Returns
- Returns 1 if the field exists, 0 otherwise. (
UInt8
)
Example
Result:
This function checks if the ‘salsa’ field exists in the JSON string. It returns 1, confirming the field’s presence.
This function:
- Searches for fields at any nesting level.
- Uses the first occurrence if multiple matching fields exist.
The JSON string should not contain space characters outside of string literals.
This function is designed for parsing a limited subset of JSON extremely fast, based on certain assumptions about the JSON structure.
simpleJSONExtractUInt
Parses a UInt64 value from a JSON string field. If the field is a string, it attempts to parse a number from the beginning of the string. If the field doesn’t exist or doesn’t contain a valid number, it returns 0.
Syntax
Arguments
json
(String
): JSON string to parse.field_name
(String
): Name of the field to extract.
Returns
- Extracted unsigned 64-bit integer value. (
UInt64
)
Example
Result:
In this example:
tacos
extracts the integer value directly.salsa
parses the number from the beginning of the string “5”.guac
returns 0 because the field doesn’t exist in the JSON.tortilla
returns 0 because the field doesn’t exist in the JSON.
The JSON string should not contain space characters outside of string literals.
This function is designed for simple JSON parsing and makes strong assumptions about the JSON structure. It searches for fields at any nesting level and uses the first occurrence if multiple fields match. For more complex JSON handling, consider using JSONExtract
functions.
simpleJSONExtractInt
Parses an Int64 value from a JSON string field. If the field is a string, it attempts to parse a number from the beginning of the string. If the field doesn’t exist or doesn’t contain a valid number, it returns 0.
Syntax
Alias:
- visitParamExtractInt
Arguments
json
(String
): JSON string to parse.field_name
(String
): Name of the field to extract.
Returns
- Returns the parsed Int64 number if the field exists and contains a valid number, 0 otherwise. [
Int64
]
Example
Result:
In this example:
taco_price
returns -4 (parsed from “-4.99”)burrito_price
returns 5 (parsed from 5.99)nacho_price
returns 0 (cannot parse “not_a_number”)quesadilla_price
returns 0 (field doesn’t exist)
Note that the function truncates floating-point numbers to integers and only parses the integer part of string values.
The JSON string should not contain space characters outside of string literals.
simpleJSONExtractFloat
Parses a Float64 value from a JSON string.
Syntax
Alias:
- visitParamExtractFloat
Arguments
json
(String
): JSON string to parse.field_name
(String
): Name of the field to extract.
Returns
- Returns the
Float64
value parsed from the specified field if it exists and contains a number. Returns 0 if the field doesn’t exist or contains a non-numeric value. [Float64
]
Example
Result:
This function attempts to parse a floating-point number from the specified field in the JSON string. If the field contains a string representation of a number (e.g., “4.99”), it will be converted to a Float64. For non-numeric fields or missing fields, the function returns 0.
This function is designed for simple JSON parsing and makes certain assumptions about the JSON structure. For more complex JSON handling, consider using the JSONExtract
family of functions.
The JSON string should not contain space characters outside of string literals.
simpleJSONExtractBool
Parses a boolean value from a JSON string. Returns 1 if the value is true, 0 otherwise.
Syntax
Arguments
json
(String
): JSON string to parse.field_name
(String
): Name of the field to extract.
Returns
- Returns 1 if the field exists and its value is true, 0 otherwise. [
UInt8
]
Example
Result:
This function returns 0 in the following cases:
- If the field doesn’t exist
- If the field contains “true” as a string (e.g.,
{"field":"true"}
) - If the field contains 1 as a numerical value
This function is designed for simple JSON parsing and makes several assumptions about the JSON structure. For more complex JSON handling, consider using the JSONExtract
functions.
The JSON string should not contain space characters outside of string literals.
simpleJSONExtractRaw
Extracts a JSON value as a raw string, including separators.
Syntax:
Alias:
- visitParamExtractRaw
Arguments:
json
(String
): JSON string to parse.field_name
(String
): Name of the field to extract.
Returns:
- Returns the value of the field as a raw string, including separators if the field exists, or an empty string otherwise. (
String
)
Example:
Result:
In this example, simpleJSONExtractRaw
extracts the ‘toppings’ array from the JSON string, preserving its structure and separators.
This function is designed for fast parsing of a simplified subset of JSON. It searches for fields at any nesting level and returns the first occurrence. The JSON must not contain spaces outside of string literals.
The JSON string should not contain space characters outside of string literals.
simpleJSONExtractString
Parses a JSON string and extracts a string value from the specified field.
Syntax:
Alias:
- visitParamExtractString
Arguments:
json
(String
): JSON string to parse.field_name
(String
): Name of the field to extract.
Returns:
- Returns the unescaped string value of the specified field if it exists and contains a string. Returns an empty string if the field doesn’t exist, contains a non-string value, or if unescaping fails. [
String
]
Example:
Result:
This function is designed for parsing a limited subset of JSON extremely fast. It makes the following assumptions:
- The field name must be a constant.
- The field name is canonically encoded in JSON.
- Fields are searched for on any nesting level, indiscriminately. If there are multiple matching fields, the first occurrence is used.
- The JSON does not have space characters outside of string literals.
There is currently no support for code points in the format \uXXXX\uYYYY that are not from the basic multilingual plane (they are converted to CESU-8 instead of UTF-8).
The JSON string should not contain space characters outside of string literals.
Was this page helpful?