7

How do I get all the empty records for a jsonb column with Active Record ?

4
  • Are you looking for all rows where the jsonb column is null? Or, are you looking for all rows where the jsonb column is an empty object '{}'? Some example code/sql would help here. Commented Jun 8, 2016 at 14:24
  • I am looking for all the rows where the jsonb column is an empty object '{}' . I actually tried Active Record query interface Model.where.not(my_column: {}) Commented Jun 9, 2016 at 9:56
  • Have you tried Model.where("my_column = '{}'") Commented Jun 9, 2016 at 10:12
  • I'll add it as an answer then. Commented Jun 9, 2016 at 11:02

2 Answers 2

18

You can query for empty objects in a JSONB column using the following syntax:

Model.where("column = '{}'")
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this by converting the json to string, and count the length. Empty json will return 2. This works for both {} and []. E.g. To return empty comment

Model.where("length(column::text) <= 2").count

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.