Skip to main content

Is this a good fit for storing JSON in database?

General • Asked by Daniel Weaver

I've got a Video model that represents a video created by the user. During creation of a video a couple of filters with different options can be applied to the video. Currently only 2 but I'll be adding more filters soon.

Would this be a good case for storing the filter options in the database as a single JSON column in the databse (Postgres)?

At first I thought of adding boolean and string/integer columns for each filter (and filter option) but even with just 2 filters this gets pretty verbose:

spectrum: boolean => true/false
spectrum_color: string => white/black
spectrum_width: string => thin/think
spin: boolean => true/false
spin_radius: integer => 200/400/800

Seems much better to store this as a JSON object. Any gotchas I should look out for?


Seems like a perfect use case to me. You can add / remove options easily in the future without adding more migrations and that'll make maintaining the filter and options a lot easier.

You might look into some of the gems that enforce types on the options if you need to make sure things are cast to booleans, ints, etc.


Thanks Chris. I found your video on this right after I posted. https://gorails.com/episodes/preferences-settings-with-activerecord-store

Looking at the typed_store gem now 👍 https://github.com/byroot/activerecord-typedstore


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.