-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathjson_transform.sql
More file actions
42 lines (37 loc) · 1.13 KB
/
json_transform.sql
File metadata and controls
42 lines (37 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
-- create a table with a json column.
create table employees (
id number generated always as identity primary key,
data json
);
-- insert a json object in the employees table
-- using json_object and json_array to build a document.
insert into employees (data) values (
json_object(
'name' value 'alice johnson',
'skills' value json_array('sql', 'git'),
'contact' value json_object(
'email' value 'alice.johnson@example.com',
'phone' value '123-456-7890'
)
)
);
-- Update JSON data on the fly with JSON_TRANSFORM:
update employees
set data = json_transform(
data,
-- add a 'position' field.
set '$.position' = 'software engineer',
-- add a new skill to the skills array.
append '$.skills' = 'java',
-- rename the name field to fullname.
rename '$.name' = 'fullname'
)
-- filter with JSON_EXISTS and a jsonpath expression
where json_exists(data, '$.name?(@ == "alice johnson")');
-- pretty print
select json_serialize(e.data pretty) as emp from employees e;
-- {
-- "position" : "Software Engineer",
-- "fullname" : "Alice Johnson",
-- ...
-- }