:
Operator (tanda titik dua)
Berlaku untuk: Databricks SQL Databricks Runtime
Mengekstrak konten dari string JSON menggunakan ekspresi jalur JSON.
Sintaks
jsonExpr : jsonPath
Argumen
jsonExpr
: EkspresiVARIANT
atauSTRING
ekspresi dengan JSON yang valid.jsonPath
: Ekspresi jalur JSON.
Mengembalikan
Hasilnya cocok dengan jenis jsonExpr
. Jika input bukan JSON yang valid atau ekspresi jalur tidak valid untuk nilai JSON hasilnya adalah NULL
.
Jika nilai yang diekstraksi adalah tidak dibatasi null
hasilnya adalah nilai NULL
.
Lihat ekspresi jalur JSON untuk penjelasan terperinci tentang jalur JSON yang valid.
Contoh
> 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