跳到主要内容

Semi-Structured Functions

This section provides reference information for the semi-structured data functions in Databend. These functions allow you to work with JSON and other semi-structured data formats efficiently.

Parsing and Validation

FunctionDescriptionExample
PARSE_JSONParses a JSON string into a variant valuePARSE_JSON('{"name":"Databend"}')
CHECK_JSONValidates if a string is valid JSONCHECK_JSON('{"name":"Databend"}')true

Object Access and Extraction

FunctionDescriptionExample
GETGets a value from a JSON object by keyGET(parse_json('{"name":"Databend"}'), 'name')'Databend'
GET_PATHGets a value from a JSON object by pathGET_PATH(parse_json('{"user":{"name":"Databend"}}'), 'user.name')'Databend'
GET_IGNORE_CASEGets a value with case-insensitive key matchingGET_IGNORE_CASE(parse_json('{"Name":"Databend"}'), 'name')'Databend'
OBJECT_KEYSReturns keys of a JSON objectOBJECT_KEYS(parse_json('{"a":1,"b":2}'))['a', 'b']
JSON_OBJECT_KEYSReturns keys of a JSON object as an arrayJSON_OBJECT_KEYS(parse_json('{"a":1,"b":2}'))['a', 'b']

Type Inspection and Conversion

FunctionDescriptionExample
JSON_TYPEOFReturns the type of a JSON valueJSON_TYPEOF(parse_json('123'))'number'
AS_TYPEConverts a JSON value to a specified SQL typeAS_TYPE(parse_json('123'), 'INT')123
JSON_TO_STRINGConverts a JSON value to a stringJSON_TO_STRING(parse_json('{"a":1}'))'{"a":1}'
IS_OBJECTChecks if a JSON value is an objectIS_OBJECT(parse_json('{"a":1}'))true
IS_ARRAYChecks if a JSON value is an arrayIS_ARRAY(parse_json('[1,2,3]'))true
IS_STRINGChecks if a JSON value is a stringIS_STRING(parse_json('"hello"'))true
IS_INTEGERChecks if a JSON value is an integerIS_INTEGER(parse_json('123'))true
IS_FLOATChecks if a JSON value is a floating-point numberIS_FLOAT(parse_json('123.45'))true
IS_BOOLEANChecks if a JSON value is a booleanIS_BOOLEAN(parse_json('true'))true
IS_NULL_VALUEChecks if a JSON value is nullIS_NULL_VALUE(parse_json('null'))true

JSON Object Operations

FunctionDescriptionExample
JSON_OBJECTCreates a JSON object from key-value pairsJSON_OBJECT('name', 'Databend', 'version', '1.0')'{"name":"Databend","version":"1.0"}'
JSON_OBJECT_INSERTInserts a value into a JSON objectJSON_OBJECT_INSERT(parse_json('{"a":1}'), 'b', 2)'{"a":1,"b":2}'
JSON_OBJECT_DELETEDeletes a key from a JSON objectJSON_OBJECT_DELETE(parse_json('{"a":1,"b":2}'), 'b')'{"a":1}'
JSON_OBJECT_PICKCreates a new object with selected keysJSON_OBJECT_PICK(parse_json('{"a":1,"b":2,"c":3}'), 'a', 'c')'{"a":1,"c":3}'
JSON_STRIP_NULLSRemoves null values from a JSON objectJSON_STRIP_NULLS(parse_json('{"a":1,"b":null}'))'{"a":1}'
JSON_OBJECT_KEEP_NULLCreates a JSON object preserving null valuesJSON_OBJECT_KEEP_NULL('a', 1, 'b', NULL)'{"a":1,"b":null}'

JSON Array Operations

FunctionDescriptionExample
JSON_ARRAYCreates a JSON array from input valuesJSON_ARRAY(1, 'text', true)'[1,"text",true]'
JSON_ARRAY_INSERTInserts a value into a JSON arrayJSON_ARRAY_INSERT(parse_json('[1,3]'), 1, 2)'[1,2,3]'
JSON_ARRAY_DISTINCTReturns an array with distinct elementsJSON_ARRAY_DISTINCT(parse_json('[1,2,1,3,2]'))'[1,2,3]'
FLATTENFlattens nested arrays into a single arrayFLATTEN(parse_json('[[1,2],[3,4]]'))'[1,2,3,4]'

Path Queries

FunctionDescriptionExample
JSON_PATH_EXISTSChecks if a JSON path existsJSON_PATH_EXISTS(parse_json('{"a":{"b":1}}'), '$.a.b')true
JSON_PATH_QUERYQueries JSON data using a path expressionJSON_PATH_QUERY(parse_json('{"a":[1,2,3]}'), '$.a[*]')[1,2,3]
JSON_PATH_QUERY_FIRSTReturns the first match from a path queryJSON_PATH_QUERY_FIRST(parse_json('{"a":[1,2,3]}'), '$.a[*]')1
JSON_PATH_QUERY_ARRAYReturns query results as a JSON arrayJSON_PATH_QUERY_ARRAY(parse_json('{"a":[1,2,3]}'), '$.a[*]')'[1,2,3]'
JSON_EXTRACT_PATH_TEXTExtracts text from a JSON pathJSON_EXTRACT_PATH_TEXT(parse_json('{"a":{"b":"text"}}'), 'a', 'b')'text'
JSON_PATH_MATCHMatches JSON data against a path expressionJSON_PATH_MATCH(parse_json('{"a":1}'), '$.a == 1')true
JQProvides jq-like JSON processing capabilitiesJQ(parse_json('{"a":{"b":1}}'), '.a.b')1

Array Transformations

FunctionDescriptionExample
JSON_ARRAY_MAPMaps a function over array elementsJSON_ARRAY_MAP(parse_json('[1,2,3]'), x -> x * 2)'[2,4,6]'
JSON_ARRAY_FILTERFilters array elements using a conditionJSON_ARRAY_FILTER(parse_json('[1,2,3,4]'), x -> x > 2)'[3,4]'
JSON_ARRAY_TRANSFORMTransforms array elements using an expressionJSON_ARRAY_TRANSFORM(parse_json('[{"a":1},{"a":2}]'), x -> x.a)'[1,2]'
JSON_ARRAY_APPLYApplies a function to each array elementJSON_ARRAY_APPLY(parse_json('[1,2,3]'), x -> x * x)'[1,4,9]'
JSON_ARRAY_REDUCEReduces an array to a single valueJSON_ARRAY_REDUCE(parse_json('[1,2,3]'), 0, (acc, x) -> acc + x)6

Set Operations

FunctionDescriptionExample
JSON_ARRAY_INTERSECTIONReturns common elements between arraysJSON_ARRAY_INTERSECTION(parse_json('[1,2,3]'), parse_json('[2,3,4]'))'[2,3]'
JSON_ARRAY_EXCEPTReturns elements in first array but not in secondJSON_ARRAY_EXCEPT(parse_json('[1,2,3]'), parse_json('[2,3,4]'))'[1]'
JSON_ARRAY_OVERLAPChecks if arrays have common elementsJSON_ARRAY_OVERLAP(parse_json('[1,2,3]'), parse_json('[3,4,5]'))true

Object Transformations

FunctionDescriptionExample
JSON_MAP_FILTERFilters key-value pairs in a JSON objectJSON_MAP_FILTER(parse_json('{"a":1,"b":2}'), (k,v) -> v > 1)'{"b":2}'
JSON_MAP_TRANSFORM_KEYSTransforms keys in a JSON objectJSON_MAP_TRANSFORM_KEYS(parse_json('{"a":1,"b":2}'), k -> UPPER(k))'{"A":1,"B":2}'
JSON_MAP_TRANSFORM_VALUESTransforms values in a JSON objectJSON_MAP_TRANSFORM_VALUES(parse_json('{"a":1,"b":2}'), v -> v * 10)'{"a":10,"b":20}'

Expansion and Formatting

FunctionDescriptionExample
JSON_ARRAY_ELEMENTSExpands a JSON array to a set of rowsSELECT * FROM JSON_ARRAY_ELEMENTS(parse_json('[1,2,3]'))3 rows with values 1, 2, 3
JSON_EACHExpands the outermost JSON object into key-value pairsSELECT * FROM JSON_EACH(parse_json('{"a":1,"b":2}'))2 rows with key-value pairs
JSON_PRETTYFormats JSON with indentation for readabilityJSON_PRETTY(parse_json('{"a":1,"b":2}'))'{\n "a": 1,\n "b": 2\n}'