FROM_JSON
Description
The FROM_JSON
function is used to parse a JSON formatted string (json_string
) and extract the corresponding data based on the provided schema definition (schema
). During the parsing process, JSON fields not described in the schema will be ignored, and fields defined in the schema that do not match the JSON fields will have their values set to NULL
. If json_string
is not in a valid JSON format, the function will return NULL
.
The syntax for schema definition is the same as when creating a table, supporting the following types:
- Array type:
array<T>
, whereT
is the type of the array elements. - Key-value pair type:
map<K, V>
, whereK
is the type of the key andV
is the type of the value. - Struct type:
struct<f1:T1, f2:T2, ... fn:Tn>
, wheref1, f2, ... fn
are field names andT1, T2, ... Tn
are field types.
Type mapping (JSON type to LakeHouse type):
- JSON object can be converted to LakeHouse's struct, map, or string.
- JSON array can be converted to LakeHouse's array or string.
- Numeric types can be converted to LakeHouse's tinyint, smallint, int, bigint, float, double, decimal, or string.
- Boolean type can be converted to LakeHouse's boolean or string.
- String type can be converted to LakeHouse's string, char, varchar, binary, date, or timestamp.
- JSON
null
value can be converted to any type.
For floating-point types (float, double), the FROM_JSON
function will try to ensure parsing precision; for decimal types, it can guarantee precision within the defined range.
Parameter Description
json_string
: Typestring
, represents the text containing the JSON string.schema
: Typestring
, defines the expected data structure to be extracted, similar to the syntax used when creating a table.
Return Type
The return type is consistent with the type described in the schema definition (schema
).
Usage Example
Notes
- Ensure that the provided JSON string is valid, otherwise the function will return
NULL
. - The schema definition should match the data structure in the JSON string, otherwise some fields may not be parsed correctly.
- When dealing with floating-point numbers and decimals, be aware that precision may be affected.
- When using the
from_json
function to extract data with a specified schema from a JSON string, case-sensitive keys in the JSON string may cause errors. For example:
To avoid this error, it is recommended to use the parse_json
function. The parse_json
function can correctly handle case-sensitive JSON keys and provides a more flexible way to access data.