Skip to content

Commit d7c42cb

Browse files
committed
Support :in_arrays option for Postgres::JSON{,B}#strip_nulls in pg_json_ops extension on PostgreSQL 18+
1 parent 76eab08 commit d7c42cb

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
=== master
22

3+
* Support :in_arrays option for Postgres::JSON{,B}#strip_nulls in pg_json_ops extension on PostgreSQL 18+ (jeremyevans)
4+
35
* Support VIRTUAL generated columns on PostgreSQL 18+ using :virtual option (jeremyevans)
46

57
=== 5.95.1 (2025-08-05)

lib/sequel/extensions/pg_json_ops.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@
166166
# # "d" date EXISTS FALSE ON ERROR
167167
# # ))
168168
#
169+
# On PostgreSQL 18+, strip_nulls can take an argument for whether to strip in arrays
170+
#
171+
# j.strip_nulls(true) # json_strip_nulls(json_column, true)
172+
# j.strip_nulls(false) # json_strip_nulls(json_column, false)
173+
#
169174
# If you are also using the pg_json extension, you should load it before
170175
# loading this extension. Doing so will allow you to use the #op method on
171176
# JSONHash, JSONHarray, JSONBHash, and JSONBArray, allowing you to perform json/jsonb operations
@@ -380,11 +385,22 @@ def query(path, opts=OPTS)
380385
self.class.new(JSONQueryOp.new(self, path, opts))
381386
end
382387

383-
# Returns a json value stripped of all internal null values.
388+
# Returns a json value stripped of all internal null values. Options:
389+
#
390+
# :in_arrays :: Whether to strip null values in JSON arrays
384391
#
385-
# json_op.strip_nulls # json_strip_nulls(json)
386-
def strip_nulls
387-
self.class.new(function(:strip_nulls))
392+
# json_op.strip_nulls # json_strip_nulls(json)
393+
# json_op.strip_nulls(in_arrays: true) # json_strip_nulls(json, true)
394+
# json_op.strip_nulls(in_arrays: false) # json_strip_nulls(json, false)
395+
def strip_nulls(opts=OPTS)
396+
in_arrays = opts[:in_arrays]
397+
f = if in_arrays.nil?
398+
function(:strip_nulls)
399+
else
400+
function(:strip_nulls, in_arrays)
401+
end
402+
403+
self.class.new(f)
388404
end
389405

390406
# Returns json_table SQL function expression, querying JSON data and returning

spec/adapters/postgres_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4863,6 +4863,11 @@ def left_item_id
48634863
@db.from(j.table(Sequel['$.a.n'].as(:foo)){Integer :b}).all.must_equal [{:b=>3}]
48644864
@db.from(j.table(Sequel['$.a'].as(:foo)){nested(Sequel['$.n'].as(:c)){Integer :b}}).all.must_equal [{:b=>3}]
48654865
end if DB.server_version >= 170000
4866+
4867+
it '18+ jsonb strip_nulls in_arrays option with pg_json_ops' do
4868+
@db.get(pg_json.call([nil, 2, 3]).op.strip_nulls(in_arrays: false)[1]).must_equal 2
4869+
@db.get(pg_json.call([nil, 2, 3]).op.strip_nulls(in_arrays: true)[1]).must_equal 3
4870+
end if DB.server_version >= 180000
48664871
end
48674872
end if DB.server_version >= 90200
48684873

spec/extensions/pg_json_ops_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ def @db.server_version(*); 130000; end
204204
@l[@jb.strip_nulls].must_equal "jsonb_strip_nulls(j)"
205205
end
206206

207+
it "should have #strip_nulls respect the in_arrays option" do
208+
@l[@j.strip_nulls(in_arrays: true)].must_equal "json_strip_nulls(j, true)"
209+
@l[@jb.strip_nulls(in_arrays: false)].must_equal "jsonb_strip_nulls(j, false)"
210+
end
211+
207212
it "should have #typeof use the json_typeof function" do
208213
@l[@j.typeof].must_equal "json_typeof(j)"
209214
@l[@jb.typeof].must_equal "jsonb_typeof(j)"

0 commit comments

Comments
 (0)