共用方式為


: (冒號) 運算符

適用於: 核取記號為「是」Databricks SQL 核取記號為「是」Databricks Runtime

使用 JSON 路徑表示式從 JSON 字串擷取內容。

語法

jsonExpr : jsonPath

引數

傳回

結果符合的類型 jsonExpr。 如果輸入無效的 JSON,或路徑表示式對 JSON 值無效,則結果為 NULL。 如果擷取的值是未分隔 null 的值,結果就是 NULL 值。

如需有效 JSON 路徑的詳細說明,請參閱 JSON 路徑表達式

範例

> SELECT c1:price
    FROM VALUES('{ "price": 5 }') AS T(c1);
 5

> SELECT c1:['price']::decimal(5,2)
    FROM VALUES('{ "price": 5 }') AS T(c1);
 5.00

> SELECT c1:item[1].price::double
    FROM VALUES('{ "item": [ { "model" : "basic", "price" : 6.12 },
                             { "model" : "medium", "price" : 9.24 } ] }') AS T(c1);
 9.24

> SELECT c1:item[*].price
    FROM VALUES('{ "item": [ { "model" : "basic", "price" : 6.12 },
                             { "model" : "medium", "price" : 9.24 } ] }') AS T(c1);
 [6.12,9.24]

> SELECT from_json(c1:item[*].price, 'ARRAY<DOUBLE>')[0]
      FROM VALUES('{ "item": [ { "model" : "basic", "price" : 6.12 },
                               { "model" : "medium", "price" : 9.24 } ] }') AS T(c1);
  6.12

> SELECT from_json(c1:item[*], 'ARRAY<STRUCT<model STRING, price DOUBLE>>')
      FROM VALUES('{ "item": [ { "model" : "basic", "price" : 6.12 },
                               { "model" : "medium", "price" : 9.24 } ] }') AS T(c1);
  [{"model":"basic","price":6.12},{"model":"medium","price":9.24}]

> SELECT inline(from_json(c1:item[*], 'ARRAY<STRUCT<model STRING, price DOUBLE>>'))
    FROM VALUES('{ "item": [ { "model" : "basic", "price" : 6.12 },
                               { "model" : "medium", "price" : 9.24 } ] }') AS T(c1);
  basic     6.12
  medium    9.24

-- Examples with VARIANT expressions.
> SELECT PARSE_JSON('{ "price": 5 }'):price
 5

> SELECT PARSE_JSON('{ "price": 5 }'):price::decimal(5,2)
 5.00

> SELECT PARSE_JSON('{ "item": [ { "model" : "basic", "price" : 6.12 },
                             { "model" : "medium", "price" : 9.24 } ] }'):item[1].price::double
 9.24