Load, Query And Process JSON Data In Snowflake
Load, Query And Process JSON Data In Snowflake
Data In Snowflake
Summary
Once you finish this video, you will be able to answer following questions:
1 {
2 "employee": {
3 "name": "John",
4 "age": 30,
5 "height_in_ft": 5.11,
6 "married": true,
7 "has_kids": false,
8 "stock_options": null,
9 "phone": "+1 123-456-7890",
10 "date_of_birth":"2022-12-08",
11 "dob_timestamp":"2022-12-08T19:27:40.606-08:00",
12 "Address": {
13 "street": "4132 Grant Street",
14 "city": "Rockville",
15 "State": "Minnesota"
16 }
17 }
18 }
A number
A string
A Boolean
An array (The Phone Number in Above example is of an arrya type)
A JSON object (The address in above example is an object type)
Null (The stock option is an example of null value)
Load JSON Data Using Insert Queries
You can directly load JSON data into relational tables using Snowflake JSON.
Then, without having to perform any modifications, you may query this data
with SQL and join it to other structured data. This enables businesses to
streamline their data pipelines and quicken the rate at which this data is
made available for analysis.
1
-- create an employee table with one variant column
2
3
create or replace transient table employee_tbl(
4
emp_json variant
5
);
6
7
-- let me insert one single record
8
insert into employee_tbl(emp_json)
9
select parse_json(
10
'{
11
"name": "John",
12
"age": 30,
13
"height_in_ft": 5.11,
14
"married": true,
15
"has_kids": false,
16
"stock_options": null
17
}');
18
19
-- lets run the select sql
20
select * from employee_tbl;
21
22
Colon Notation To Access JSON Elements
1
select
2
emp_json:name,
3
emp_json:"age",
4
emp_json:height_in_ft,
5
emp_json:married,
6
emp_json:has_kids,
7
emp_json:stock_options
8
from employee_tbl;
9
10
1 select
2
3 emp_json:age::integer as age,
4 emp_json:name::string as "Name",
5 emp_json:height_in_ft::decimal as height,
6 emp_json:married as is_married,
7 emp_json:has_kids as has_kids,
8 emp_json:stock_options as stock_options
9 from employee_tbl;
10
11 -- add typeof() funciton to check how snowflake treat these name/value pair
12 select
13 typeof(emp_json:name) as name,
14 typeof(emp_json:age) as age,
15 typeof(emp_json:height_in_ft) as height,
16 typeof(emp_json:married) as is_married,
17 typeof(emp_json:has_kids) as has_kids,
18 typeof(emp_json:stock_options) as stock_options
19 from employee_tbl;
20
21
22 insert into employee_tbl(emp_json)
23 select parse_json(
24 '{
25 "employee": {
26 "name": "John",
27 "age": 30,
28 "height_in_ft": 5.11,
29 "married": true,
30 "has_kids": false,
31 "stock_options": null,
32 "phone": [
33 "+1 123-456-7890",
34 "+1 098-765-4321"
35 ],
36 "Address": {
37 "street": "3621 McDonald Avenue",
38 "city": "Orlando",
39 "State": "Florida"
40 }
41 }
42 }');
43
44 select * from employee_tbl;
45
46 select
47 emp_json:employee.name::string as name,
48 emp_json:employee.age as age,
49 emp_json:employee.height_in_ft as height,
50 emp_json:employee.married as is_married,
51 emp_json:employee.has_kids as has_kids,
52 emp_json:employee.stock_options as stock_options,
53 typeof(emp_json:employee.phone) as all_phone_type,
54 ARRAY_SIZE(emp_json:employee.phone) as how_many_phone,
55 emp_json:employee.phone[0] as work_phone,
56 emp_json:employee.phone[1] as office_phone,
57 typeof(emp_json:employee:Address) as address_type,
58 emp_json:employee:Address:street as street,
59 emp_json:employee.Address.city as city,
60 emp_json:employee.Address.State as state
61 from employee_tbl;
62
63
64 -- apply other function and mathematical operation without casting it.
65 select
66 emp_json:employee.age as age,
67 (emp_json:employee.height_in_ft) * (12*2.54) as height_in_cm,
68 typeof(emp_json:employee.Phone) as all_phone_type,
69 ARRAY_SIZE(emp_json:employee.Phone) as how_many_phone
70 from employee_tbl;
71
1 -- create sequencer
2 create or replace sequence emp_seq
3 start 1
4 increment 1
5 comment = 'employee sequence';
6
7 create or replace sequence phone_seq
8 start 1
9 increment 1
10 comment = 'phone sequence';
11
12 create or replace sequence address_seq
13 start 1
14 increment 1
15 comment = 'address sequence';
16