:(冒号)运算符

适用于:勾选“是” Databricks SQL 勾选“是” Databricks Runtime

使用 JSON 路径表达式从 JSON 字符串中提取内容。

语法

jsonExpr : jsonPath

参数

  • jsonExpr:使用有效 JSON 的 VARIANT 表达式或 STRING 表达式。
  • jsonPathJSON 路径表达式

返回

结果与类型 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