title: 'JSON Functions and Operators'
sidebar_label: 'JSON'
description: 'Reference for JSON functions and operators in Spice SQL'
sidebar_position: 7
JSON support in Spice is based on datafusion-functions-json, which provides functions and operators to extract, query, and manipulate JSON data stored as strings. Advanced features for JSON creation, modification, or complex path expressions are not supported.
:::warning[Limitations]
- JSON functions and operators are supported only during DataFusion (Arrow) execution.
- Federated or accelerated sources (non-Arrow) may not support all JSON functions.
:::
JSON Functions
Enables extracting and manipulating data from JSON strings. Each function takes a JSON string as the first argument, followed by one or more keys or indices to specify the path.
json_contains
Returns true if a JSON string contains a specific key at the specified path.
Arguments
- json_string: String containing valid JSON data.
- key1, key2, ...: Path to the key to check. Can be string keys for objects or integer indices for arrays.
Example
json_get
Retrieves a value from a JSON string based on its path.
Arguments
- json_string: String containing valid JSON data.
- key1, key2, ...: Path to the value. Can be string keys for objects or integer indices for arrays.
Example
json_get_str
Retrieves a string value from a JSON string based on its path.
Arguments
- json_string: String containing valid JSON data.
- key1, key2, ...: Path to the string value.
Example
json_get_int
Retrieves an integer value from a JSON string based on its path.
Arguments
- json_string: String containing valid JSON data.
- key1, key2, ...: Path to the integer value.
Example
json_get_float
Retrieves a float value from a JSON string based on its path.
Arguments
- json_string: String containing valid JSON data.
- key1, key2, ...: Path to the float value.
Example
json_get_bool
Retrieves a boolean value from a JSON string based on its path.
Arguments
- json_string: String containing valid JSON data.
- key1, key2, ...: Path to the boolean value.
Example
json_get_json
Retrieves a nested JSON object or array as a raw JSON string from a JSON string based on its path.
Arguments
- json_string: String containing valid JSON data.
- key1, key2, ...: Path to the nested JSON value.
Example
json_get_array
Retrieves an arrow array from a JSON string based on its path.
Arguments
- json_string: String containing valid JSON data.
- key1, key2, ...: Path to the array value.
Example
json_as_text
Retrieves any value from a JSON string based on its path and represents it as a string. This is useful for converting JSON values to text format.
Arguments
- json_string: String containing valid JSON data.
- key1, key2, ...: Path to the value to convert to text.
Example
json_length
Returns the length of a JSON string, array, or object. For objects, returns the number of key-value pairs. For arrays, returns the number of elements. For strings, returns the character count.
Arguments
- json_string: String containing valid JSON data.
- key1, key2, ...: Optional path to a nested value. If omitted, returns the length of the root JSON value.
Example
JSON Operators
-> {#op_json_get}
JSON access operator. Retrieves a value from a JSON string based on its path. This operator is an alias for json_get.
Arguments
- json_string: String containing valid JSON data.
- key: Object key (string) or array index (integer).
Example
->> {#op_json_as_text}
JSON access operator for text extraction. Retrieves any value from a JSON string and converts it to text. This operator is an alias for json_as_text.
Arguments
- json_string: String containing valid JSON data.
- key: Object key (string) or array index (integer).
Example
? {#op_json_contains}
JSON containment operator. Returns true if a JSON string contains the specified key. This operator is an alias for json_contains.
Arguments
- json_string: String containing valid JSON data.
- key: Key to check for existence.
Example
Usage Examples
Nested Object Access
Array Access
Conditional JSON Queries
Using JSON Functions in Views
JSON functions can be used in views to simplify access to nested JSON data:
Further Reading