JSON_EXTRACT_PATH_TEXT 函數 - AWS Clean Rooms

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

JSON_EXTRACT_PATH_TEXT 函數

該 JSON_EXTRACT_PATH_TEXT 函數返回由一系列 JSON 字符串中的路徑元素引用的鍵:值對的值。JSON 路徑巢狀最深可達五層。路徑元素區分大小寫。如果 JSON 字串中沒有路徑元素,JSON_EXTRACT_PATH_TEXT 會傳回空字串。如果 null_if_invalid 引數設為 true,且 JSON 字串無效,此函數會傳回 Null,而非傳回錯誤。

如需有關其他 JSON 函數的資訊,請參閱JSON 函數

語法

json_extract_path_text('json_string', 'path_elem' [,'path_elem'[, …] ] [, null_if_invalid ] )

引數

json_string

格式正確的 JSON 字串。

path_elem

JSON 字串中的路徑元素。需要一個路徑元素。可指定其他路徑元素,最深可達五層。

null_if_invalid

布林值,指定輸入 JSON 字串無效時是否傳回 Null,而非傳回錯誤。若要在 JSON 無效時傳回 Null,請指定 true (t)。若要在 JSON 無效時傳回錯誤,請指定 false (f)。預設值為 false

在 JSON 字串中,AWS Clean Rooms 將 \n 視為新行字元,將 \t 視為 Tab 字元。若要載入反斜線,請加上反斜線 ( \\ ) 來逸出。

傳回類型

VARCHAR 字串,代表路徑元素所參考的 JSON 值。

範例

下列範例會傳回路徑的值'f4', 'f6'

select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}}','f4', 'f6'); json_extract_path_text ---------------------- star

下列範例會傳回錯誤,因為 JSON 無效。

select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}','f4', 'f6'); An error occurred when executing the SQL command: select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}','f4', 'f6')

下列範例將 null_if_invalid 設為 true,所以在 JSON 無效時,陳述式會傳回 Null,而非傳回錯誤。

select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}','f4', 'f6',true); json_extract_path_text ------------------------------- NULL

下列範例會傳回路徑的值'farm', 'barn', 'color',其中擷取的值位於第三層。此範例使用 JSON lint 工具進行格式化,以便於閱讀。

select json_extract_path_text('{ "farm": { "barn": { "color": "red", "feed stocked": true } } }', 'farm', 'barn', 'color'); json_extract_path_text ---------------------- red

下列範例會傳回 NULL,因為'color'元素遺失。此範例使用 JSON 棉絨工具進行格式化。

select json_extract_path_text('{ "farm": { "barn": {} } }', 'farm', 'barn', 'color'); json_extract_path_text ---------------------- NULL

如果 JSON 是有效的,嘗試提取缺少的元素返回 NULL。

下列範例會傳回路徑的值'house', 'appliances', 'washing machine', 'brand'

select json_extract_path_text('{ "house": { "address": { "street": "123 Any St.", "city": "Any Town", "state": "FL", "zip": "32830" }, "bathroom": { "color": "green", "shower": true }, "appliances": { "washing machine": { "brand": "Any Brand", "color": "beige" }, "dryer": { "brand": "Any Brand", "color": "white" } } } }', 'house', 'appliances', 'washing machine', 'brand'); json_extract_path_text ---------------------- Any Brand