# PostgreSQL 18.6 on x86_64-pc-linux-gnu == PART 1: default settings (hibernate.query.hql.json_functions_enabled not set) == default: json_value hql> select p.name, json_value(p.attributes, '$.color') from Product p where p.id = 1 FAILED IllegalArgumentException: Can't use function 'json_value', because tech preview JSON functions are not enabled. To enable, set the 'hibernate.query.hql.json_functions_enabled' setting to 'true'. [select p.name, json_value(p.attributes, '$.color') from Product p where p.id = 1] == default: json_exists hql> select p.name from Product p where json_exists(p.attributes, '$.color') FAILED IllegalArgumentException: Can't use function 'json_exists', because tech preview JSON functions are not enabled. To enable, set the 'hibernate.query.hql.json_functions_enabled' setting to 'true'. [select p.name from Product p where json_exists(p.attributes, '$.color')] == default: json_set (UPDATE) hql> update Product p set p.attributes = json_set(p.attributes, '$.color', 'green') where p.id = 1 sql> update product p1_0 set attributes=jsonb_set(p1_0.attributes,array['color']::text[],to_jsonb('green'::text),true) where p1_0.id=1 OK, 1 row(s) updated == PART 2: hibernate.query.hql.json_functions_enabled=true == json_value, string hql> select p.name, json_value(p.attributes, '$.color') from Product p order by p.id sql> select p1_0.name,json_value(p1_0.attributes,'$.color' returning jsonb)#>>'{}' from product p1_0 order by p1_0.id OK (Desk lamp, red); (Floor lamp, black); (Office chair, black); (Bookshelf, oak); (Monitor arm, silver) == json_value, returning integer hql> select p.name, json_value(p.attributes, '$.stock' returning integer) from Product p order by p.id sql> select p1_0.name,json_value(p1_0.attributes,'$.stock' returning integer) from product p1_0 order by p1_0.id OK (Desk lamp, 12); (Floor lamp, 3); (Office chair, 0); (Bookshelf, 7); (Monitor arm, 25) == json_value in where hql> select p.name from Product p where json_value(p.attributes, '$.color') = 'black' order by p.id sql> select p1_0.name from product p1_0 where json_value(p1_0.attributes,'$.color' returning jsonb)#>>'{}'='black' order by p1_0.id OK Floor lamp; Office chair == json_value nested path + array index hql> select p.name, json_value(p.attributes, '$.nested.aisles[1]' returning integer) from Product p order by p.id sql> select p1_0.name,json_value(p1_0.attributes,'$.nested.aisles[1]' returning integer) from product p1_0 order by p1_0.id OK (Desk lamp, 7); (Floor lamp, 7); (Office chair, 7); (Bookshelf, 7); (Monitor arm, 7) == json_value on a record column hql> select p.name, json_value(p.dimensions, '$.widthMm' returning integer) from Product p order by p.id sql> select p1_0.name,json_value(p1_0.dimensions,'$.widthMm' returning integer) from product p1_0 order by p1_0.id OK (Desk lamp, 180); (Floor lamp, 300); (Office chair, 600); (Bookshelf, 900); (Monitor arm, 150) == json_value on a list column hql> select p.name, json_value(p.tags, '$[0]') from Product p order by p.id sql> select p1_0.name,json_value(p1_0.tags,'$[0]' returning jsonb)#>>'{}' from product p1_0 order by p1_0.id OK (Desk lamp, lighting); (Floor lamp, lighting); (Office chair, office); (Bookshelf, furniture); (Monitor arm, office) == json_value, missing key hql> select p.name, json_value(p.attributes, '$.nope') from Product p where p.id = 1 sql> select p1_0.name,json_value(p1_0.attributes,'$.nope' returning jsonb)#>>'{}' from product p1_0 where p1_0.id=1 OK (Desk lamp, null) == json_query (sub-document) hql> select p.name, json_query(p.attributes, '$.nested') from Product p where p.id = 1 sql> select p1_0.name,json_query(p1_0.attributes,'$.nested') from product p1_0 where p1_0.id=1 OK (Desk lamp, {"aisles": [3, 7], "warehouse": "PUNE"}) == json_query with wrapper hql> select p.name, json_query(p.attributes, '$.nested.aisles[*]' with wrapper) from Product p where p.id = 1 sql> select p1_0.name,json_query(p1_0.attributes,'$.nested.aisles[*]' with wrapper) from product p1_0 where p1_0.id=1 OK (Desk lamp, [3, 7]) == json_exists hql> select p.name from Product p where json_exists(p.attributes, '$.nested.warehouse') order by p.id sql> select p1_0.name from product p1_0 where json_exists(p1_0.attributes,'$.nested.warehouse') order by p1_0.id OK Desk lamp; Floor lamp; Office chair; Bookshelf; Monitor arm == json_exists with filter hql> select p.name from Product p where json_exists(p.attributes, '$.stock ? (@ > 5)') order by p.id sql> select p1_0.name from product p1_0 where json_exists(p1_0.attributes,'$.stock ? (@ > 5)') order by p1_0.id OK Desk lamp; Bookshelf; Monitor arm == json_object hql> select json_object('name', p.name, 'id', p.id) from Product p where p.id = 1 sql> select json_object('name' value p1_0.name,'id' value p1_0.id) from product p1_0 where p1_0.id=1 OK {"name" : "Desk lamp", "id" : 1} == json_object, key/value keywords hql> select json_object(key 'name' value p.name) from Product p where p.id = 1 sql> select json_object('name' value p1_0.name) from product p1_0 where p1_0.id=1 OK {"name" : "Desk lamp"} == json_array hql> select json_array(p.name, p.id, null) from Product p where p.id = 1 sql> select json_array(p1_0.name,p1_0.id,null) from product p1_0 where p1_0.id=1 OK ["Desk lamp", 1] == json_arrayagg hql> select json_arrayagg(p.name) from Product p sql> select json_arrayagg(p1_0.name absent on null returning jsonb) from product p1_0 OK ["Desk lamp", "Floor lamp", "Office chair", "Bookshelf", "Monitor arm"] == json_objectagg hql> select json_objectagg(key p.name value p.id) from Product p sql> select json_objectagg(p1_0.name:p1_0.id absent on null returning jsonb) from product p1_0 OK {"Bookshelf": 4, "Desk lamp": 1, "Floor lamp": 2, "Monitor arm": 5, "Office chair": 3} == json_table over a list column hql> select p.name, t.tag from Product p join lateral json_table(p.tags, '$[*]' columns (tag String path '$')) t where p.id = 1 sql> select p1_0.name,t1_0.tag from product p1_0 join lateral json_table(p1_0.tags,'$[*]' columns(tag text path '$')) t1_0 on true where p1_0.id=1 OK (Desk lamp, lighting); (Desk lamp, office) == json_table, tags counted hql> select t.tag, count(*) from Product p join lateral json_table(p.tags, '$[*]' columns (tag String path '$')) t group by t.tag order by t.tag sql> select t1_0.tag,count(*) from product p1_0 join lateral json_table(p1_0.tags,'$[*]' columns(tag text path '$')) t1_0 on true group by t1_0.tag order by t1_0.tag OK (accessories, 1); (furniture, 2); (home, 2); (lighting, 2); (office, 3) == json_set hql> update Product p set p.attributes = json_set(p.attributes, '$.color', 'green') where p.id = 1 sql> update product p1_0 set attributes=jsonb_set(p1_0.attributes,array['color']::text[],to_jsonb('green'::text),true) where p1_0.id=1 OK, 1 row(s) updated == json_insert hql> update Product p set p.attributes = json_insert(p.attributes, '$.brand', 'Acme') where p.id = 1 sql> update product p1_0 set attributes=(select case when (t.d)#>>t.p is not null then t.d else jsonb_insert(t.d,t.p,to_jsonb('Acme'::text),true) end from (values(p1_0.attributes,array['brand']::text[])) t(d,p)) where p1_0.id=1 OK, 1 row(s) updated == json_replace hql> update Product p set p.attributes = json_replace(p.attributes, '$.stock', 99) where p.id = 1 sql> update product p1_0 set attributes=jsonb_set(p1_0.attributes,array['stock']::text[],to_jsonb(99),false) where p1_0.id=1 OK, 1 row(s) updated == json_remove hql> update Product p set p.attributes = json_remove(p.attributes, '$.price') where p.id = 1 sql> update product p1_0 set attributes=p1_0.attributes#-array['price']::text[] where p1_0.id=1 OK, 1 row(s) updated == json_mergepatch hql> update Product p set p.attributes = json_mergepatch(p.attributes, '{"stock": 1, "brand": null}') where p.id = 2 sql> update product p1_0 set attributes=(with recursive args(d0,d1) as(select p1_0.attributes,cast('{"stock": 1, "brand": null}' as jsonb)),val0(p,k,v) as (select '{}'::text[],s.k,t.d0->s.k from args t join lateral jsonb_object_keys(t.d0) s(k) on 1=1 union select v.p||v.k,s.k,v.v->s.k from val0 v join lateral jsonb_object_keys(v.v) s(k) on jsonb_typeof(v.v)='object'),val1(p,k,v) as (select '{}'::text[],s.k,t.d1->s.k from args t join lateral jsonb_object_keys(t.d1) s(k) on 1=1 union select v.p||v.k,s.k,v.v->s.k from val1 v join lateral jsonb_object_keys(v.v) s(k) on jsonb_typeof(v.v)='object'),res(v,p,l) as(select jsonb_object_agg(coalesce(v1.k,v0.k),coalesce(v1.v,v0.v)),coalesce(v1.p,v0.p),cardinality(coalesce(v1.p,v0.p)) from val0 v0 full join val1 v1 on v0.p=v1.p and v0.k=v1.k where cardinality(coalesce(v1.p,v0.p))=(select cardinality(v.p) from val0 v union select cardinality(v.p) from val1 v order by 1 desc limit 1) and jsonb_typeof(coalesce(v1.v)) is distinct from 'null' group by coalesce(v1.p,v0.p),cardinality(coalesce(v1.p,v0.p)) union all select jsonb_object_agg(coalesce(v1.k,v0.k),coalesce(case when coalesce(v1.k,v0.k)=r.p[cardinality(r.p)] then r.v end,v1.v,v0.v)) filter (where coalesce(case when coalesce(v1.k,v0.k)=r.p[cardinality(r.p)] then r.v end,v1.v,v0.v) is not null),coalesce(v1.p,v0.p),r.l-1 from val0 v0 full join val1 v1 on v0.p=v1.p and v0.k=v1.k join (select * from res r order by r.l fetch first 1 rows with ties) r on cardinality(coalesce(v1.p,v0.p))=r.l-1 and jsonb_typeof(coalesce(v1.v)) is distinct from 'null' and r.l<>0 group by coalesce(v1.p,v0.p),r.l-1) select r.v from res r where r.l=0) where p1_0.id=2 OK, 1 row(s) updated == json_array_append, root path $ hql> update Product p set p.tags = json_array_append(p.tags, '$', 'sale') where p.id = 1 sql> update product p1_0 set tags=(select jsonb_set_lax(t.d,t.p,(t.d)#>t.p||to_jsonb('sale'::text),false,'return_target') from (values(p1_0.tags,array]::text[])) t(d,p)) where p1_0.id=1 FAILED SQLGrammarException: ERROR: syntax error at or near "]" == json_array_append, nested path hql> update Product p set p.attributes = json_array_append(p.attributes, '$.nested.aisles', 11) where p.id = 1 sql> update product p1_0 set attributes=(select jsonb_set_lax(t.d,t.p,(t.d)#>t.p||to_jsonb(11),false,'return_target') from (values(p1_0.attributes,array['nested','aisles']::text[])) t(d,p)) where p1_0.id=1 OK, 1 row(s) updated == json_array_insert hql> update Product p set p.tags = json_array_insert(p.tags, '$[0]', 'first') where p.id = 2 sql> update product p1_0 set tags=jsonb_insert(p1_0.tags,array['0']::text[],to_jsonb('first'::text)) where p1_0.id=2 OK, 1 row(s) updated == attribute navigation p.attributes.color hql> select p.name from Product p where p.attributes.color = 'black' FAILED IllegalArgumentException: Could not interpret attribute 'color' of basic-valued path 'com.ankurm.jsoncolumns.model.Product(p).attributes' == function('jsonb_extract_path_text') hql> select function('jsonb_extract_path_text', p.attributes, 'color') from Product p where p.id = 3 sql> select jsonb_extract_path_text(p1_0.attributes,'color') from product p1_0 where p1_0.id=3 OK black == rows after the UPDATE experiments (raw jsonb) 1 {"brand": "Acme", "color": "green", "stock": 99, "nested": {"aisles": [3, 7, 11], "warehouse": "PUNE"}} ["lighting", "office"] 2 {"color": "black", "price": 89.5, "stock": 1, "nested": {"aisles": [3, 7], "warehouse": "PUNE"}} ["first", "lighting", "home"]