You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- Create or replace the books tableCREATE OR REPLACETABLEbooks (
id BIGINTPRIMARY KEY,
title VARCHARNOT NULL DEFAULT 'Untitled',
genres_array VARCHAR[] default [],
publication_date DATE DEFAULT CURRENT_DATE,
price DECIMAL(10, 2)
);
describe books
column_name
column_type
null
key
default
extra
id
BIGINT
NO
PRI
title
VARCHAR
NO
'Untitled'
genres_array
VARCHAR[]
YES
main.list_value()
publication_date
DATE
YES
CURRENT_DATE
price
DECIMAL(10,2)
YES
create json using struct_pack() and json cast or to_json()
SELECT
struct_pack(n:=1, str:='something') as my_struct,
to_json(struct_pack(n:=1, str:='something')) as to_json_ed,
struct_pack(n:=1, str:='something')::json as casted_json
my_struct
to_json_ed
casted_json
{"n": 1, "str": "something"}
{"n":1,"str":"something"}
{"n":1,"str":"something"}
if you need to do something special cast to json from string
Eg, duckdb syntax does not support empty structs, can use cast string for that
select'{}'::json
CAST('{}' AS "json")
{}
Duckdb has native arrays
use native [] syntax for arrays
select [] as empty, [{'a':1}] as such_nice_array_of_struct, [{'a':1}]::json as wow_json, array_value({'a':1}) array_value_is_verbose, array_agg(1)