tencent cloud

Feedback

JSON Functions

Last updated: 2024-08-07 17:32:18

    GET_JSON_OBJECT

    Function statement:
    GET_JSON_OBJECT(<json> string, <path> string)
    Supported engines: SparkSQL and Presto
    Usage instructions: Extract JSON object.
    Return type: string
    Example:
    > SELECT get_json_object('{"a":"b"}', '$.a');
    b

    JSON_TUPLE

    Function statement:
    JSON_TUPLE(<json> string, <p1> string, ..., <pn> string)
    Supported engine: SparkSQL
    Usage instructions: Return a tuple similar to the function get_json_object, but it requires multiple input names. All input parameters and output column types are strings.
    Return type: struct<string, ..., string>
    Example:
    > SELECT json_tuple('{"a":1, "b":2}', 'a', 'b');
    1 2

    TO_JSON

    Function statement:
    TO_JSON(<expr> struct|map|array[, <option> map])
    Supported engines: SparkSQL and Presto
    Usage instructions: Return a JSON string with the given structured value.
    Return type: string
    Example:
    > SELECT to_json(named_struct('a', 1, 'b', 2));
    {"a":1,"b":2}
    > SELECT to_json(named_struct('time', to_timestamp('2015-08-26', 'yyyy-MM-dd')), map('timestampFormat', 'dd/MM/yyyy'));
    {"time":"26/08/2015"}
    > SELECT to_json(array(named_struct('a', 1, 'b', 2)));
    [{"a":1,"b":2}]
    > SELECT to_json(map('a', named_struct('b', 1)));
    {"a":{"b":1}}
    > SELECT to_json(map(named_struct('a', 1),named_struct('b', 2)));
    {"[1]":{"b":2}}
    > SELECT to_json(map('a', 1));
    {"a":1}
    > SELECT to_json(array((map('a', 1))));
    [{"a":1}]

    FROM_JSON

    Function statement:
    FROM_JSON(<json> string, <schema> string[, <options> map<string, string>])
    Supported engines: SparkSQL and Presto
    Usage instructions: Return a structured value with the given jsonStr and schema.
    Return type: struct
    Example:
    > SELECT from_json('{"a":1, "b":0.8}', 'a INT, b DOUBLE');
    {"a":1,"b":0.8}
    > SELECT from_json('{"time":"26/08/2015"}', 'time Timestamp', map('timestampFormat', 'dd/MM/yyyy'));
    {"time":2015-08-26 00:00:00}

    SCHEMA_OF_JSON

    Function statement:
    SCHEMA_OF_JSON(<json> string[, <options> map<string, string>])
    Supported engine: SparkSQL
    Usage instructions: Return the DDL-formatted schema of the JSON string.
    Return type: string
    Example:
    > SELECT schema_of_json('[{"col":0}]');
    ARRAY<STRUCT<`col`: BIGINT>>
    > SELECT schema_of_json('[{"col":01}]', map('allowNumericLeadingZeros', 'true'));
    ARRAY<STRUCT<`col`: BIGINT>>

    JSON_ARRAY_LENGTH

    Function statement:
    JSON_ARRAY_LENGTH(<jsonArray> string)
    Supported engines: SparkSQL and Presto
    Usage instructions: Return the number of elements in the outermost JSON array.
    Return type: integer
    Example:
    > SELECT json_array_length('[1,2,3,4]');
    4
    > SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
    5
    > SELECT json_array_length('[1,2');
    NULL

    JSON_OBJECT_KEYS

    Function statement:
    JSON_OBJECT_KEYS(<json> string)
    Supported engines: SparkSQL and Presto
    Usage instructions: Return all keys of the outermost JSON object as an array.
    Return type: array<string>
    Example:
    > SELECT json_object_keys('{}');
    []
    > SELECT json_object_keys('{"key": "value"}');
    ["key"]
    > SELECT json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}');
    ["f1","f2"]
    
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support