JSON 경로 식
적용 대상: Databricks SQL Databricks Runtime으로 표시됨
JSON 경로 식은 를 사용하여 JSON 문자열에서 값을 추출하는 데 사용됩니다.
구문
{ { identifier | [ field ] | [ * ] | [ index ] }
[ . identifier | [ field ] | [ * ] | [ index ] ] [...] }
및 *
index
를 둘러싼 field
대괄호는 실제 대괄호이며 선택적 구문을 나타내지 않습니다.
매개 변수
- identifier: JSON 필드의 대/소문자를 구분하지 않는 식별자입니다.
[ field ]
: JSON 필드를 식별하는 대/소문자 구분 STRING 리터럴입니다.[ * ]
: JSON 배열의 모든 요소를 식별합니다.[ index ]
: 0 기반 JSON 배열의 특정 요소를 식별하는 정수 리터럴입니다.
반환
문자열입니다.
구분되지 않은 null
값으로 JSON 필드가 있는 경우 텍스트 값이 아닌 null
해당 열에 대한 SQL NULL
값을 받게 됩니다.
:: 연산자 를 사용하여 값을 기본 데이터 형식으로 캐스팅할 수 있습니다.
from_json 함수를 사용하여 중첩된 결과를 배열 또는 구조체와 같은 더 복잡한 데이터 형식으로 캐스팅합니다.
메모
구분되지 않은 식별자를 사용하여 이름에 공백 또는 특수 문자가 없고 다른 경우에 동일한 이름의 필드가 없는 경우 JSON 필드를 참조할 수 있습니다.
다른 경우에 동일한 이름의 필드가 없는 경우 구분된 식별자를 사용합니다.
표기법은 [ field ]
항상 사용할 수 있지만 필드의 대/소문자를 정확히 일치해야 합니다.
Databricks SQL에서 필드를 고유하게 식별할 수 없는 경우 오류가 반환됩니다. Databricks SQL 필드에 대한 일치 항목이 없으면 가 반환됩니다 NULL
.
예
다음 예제에서는 예제 데이터의 문과 함께 만든 데이터를 사용합니다.
이 섹션에서는 다음을 수행합니다.
식별자 및 구분 기호를 사용하여 추출
> SELECT raw:owner, raw:OWNER, raw:['owner'], raw:['OWNER'] FROM store_data;
amy amy amy NULL
-- Use backticks to escape special characters. References are case insensitive when you use backticks.
-- Use brackets to make them case sensitive.
> SELECT raw:`zip code`, raw:`Zip Code`, raw:['fb:testid'] FROM store_data;
94025 94025 1234
중첩 필드 추출
-- Use dot notation
> SELECT raw:store.bicycle FROM store_data;
'{ "price":19.95, "color":"red" }'
-- Use brackets
> SELECT raw:['store']['bicycle'] FROM store_data;
'{ "price":19.95, "color":"red" }'
배열에서 값 추출
-- Index elements
> SELECT raw:store.fruit[0], raw:store.fruit[1] FROM store_data;
'{ "weight":8, "type":"apple" }' '{ "weight":9, "type":"pear" }'
-- Extract subfields from arrays
> SELECT raw:store.book[*].isbn FROM store_data;
'[ null, "0-553-21311-3", "0-395-19395-8" ]'
-- Access arrays within arrays or structs within arrays
> SELECT raw:store.basket[*],
raw:store.basket[*][0] first_of_baskets,
raw:store.basket[0][*] first_basket,
raw:store.basket[*][*] all_elements_flattened,
raw:store.basket[0][2].b subfield
FROM store_data;
basket first_of_baskets first_basket all_elements_flattened subfield
---------------------------- ------------------ --------------------- --------------------------------- ----------
[ [ [ [1,2,{"b":"y","a":"x"},3,4,5,6] y
[1,2,{"b":"y","a":"x"}], 1, 1,
[3,4], 3, 2,
[5,6] 5 {"b":"y","a":"x"}
] ] ]
NULL 동작
> SELECT '{"key":null}':key IS NULL sql_null, '{"key":"null"}':key IS NULL;
true false
캐스트 값
-- price is returned as a double, not a string
> SELECT raw:store.bicycle.price::double FROM store_data
19.95
-- use from_json to cast into more complex types
> SELECT from_json(raw:store.bicycle, 'price double, color string') bicycle FROM store_data
'{ "price":19.95, "color":"red" }'
-- the column returned is an array of string arrays
> SELECT from_json(raw:store.basket[*], 'array<array<string>>') baskets FROM store_data
'[
["1","2","{\"b\":\"y\",\"a\":\"x\"}]",
["3","4"],
["5","6"]
]'
예제 데이터
CREATE TABLE store_data AS SELECT
'{
"store":{
"fruit": [
{"weight":8,"type":"apple"},
{"weight":9,"type":"pear"}
],
"basket":[
[1,2,{"b":"y","a":"x"}],
[3,4],
[5,6]
],
"book":[
{
"author":"Nigel Rees",
"title":"Sayings of the Century",
"category":"reference",
"price":8.95
},
{
"author":"Herman Melville",
"title":"Moby Dick",
"category":"fiction",
"price":8.99,
"isbn":"0-553-21311-3"
},
{
"author":"J. R. R. Tolkien",
"title":"The Lord of the Rings",
"category":"fiction",
"reader":[
{"age":25,"name":"bob"},
{"age":26,"name":"jack"}
],
"price":22.99,
"isbn":"0-395-19395-8"
}
],
"bicycle":{
"price":19.95,
"color":"red"
}
},
"owner":"amy",
"zip code":"94025",
"fb:testid":"1234"
}' as raw