All threads / Is this a good fit for storing JSON in database?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Is this a good fit for storing JSON in database?

Daniel Weaver asked in General

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.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 35,699+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.