tencent cloud

Data Lake Compute

JSON 関数

PDF
フォーカスモード
フォントサイズ
最終更新日: 2025-12-25 12:00:07

GET_JSON_OBJECT

関数構文:
GET_JSON_OBJECT(<json> string, <path> string)
サポートエンジン:SparkSQL、Presto。
使用説明:jsonオブジェクトを抽出します。
戻り値の型:string。
例:
> SELECT get_json_object('{"a":"b"}', '$.a');
b

JSON_TUPLE

関数構文:
JSON_TUPLE(<json> string, <p1> string, ..., <pn> string)
サポートエンジン:SparkSQL。
使用説明:get_json_object関数に似たタプルを返しますが、複数の名前を入力する必要があります。すべての入力パラメータと出力列の型は文字列です。
戻り値の型:struct<string, ..., string>。
例:
> SELECT json_tuple('{"a":1, "b":2}', 'a', 'b');
1 2

TO_JSON

関数構文:
TO_JSON(<expr> struct|map|array[, <option> map])
サポートエンジン:SparkSQL、Presto。
使用説明:指定された構造値を持つJSON文字列を返します。
「設定-一般-クイックフローティングウィンドウ」で自動表示をオンにできます
例:
> 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

関数構文:
FROM_JSON(<json> string, <schema> string[, <options> map<string, string>])
サポートエンジン:SparkSQL、Presto。
使用説明:指定されたjsonStrとスキーマを持つ構造値を返します。
戻り値の型:struct。
例:
> 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

関数構文:
SCHEMA_OF_JSON(<json> string[, <options> map<string, string>])
サポートエンジン:SparkSQL。
使用説明:JSON文字列のDDL形式の構造を返します。
戻り値の型:string。
例:
> 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

関数構文:
JSON_ARRAY_LENGTH(<jsonArray> string)
サポートエンジン:SparkSQL、Presto。
使用説明:最外層のJSON配列内の要素数を返します。
戻り値の型:integer。
例:
> 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

関数構文:
JSON_OBJECT_KEYS(<json> string)
サポートエンジン:SparkSQL、Presto。
使用説明:最外層のJSONオブジェクトのすべてのキーを配列形式で返します。
戻り値の型:array<string>。
例:
> SELECT json_object_keys('{}');
[]
> SELECT json_object_keys('{"key": "value"}');
["key"]
> SELECT json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}');
["f1","f2"]


ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック