Facebook iconUpdating JSON Columns in Ruby on Rails
Blogs/Technology

Strategies for Updating JSON Columns in Ruby on Rails

Feb 9, 20246 Min Read
by Piyush Duragkar
Strategies for Updating JSON Columns in Ruby on Rails Hero

Ruby on Rails is a framework that has become popular in the dynamic field of web development for creating scalable and reliable applications. JSON columns are supported by ROR, giving developers an effective method for working with deeply nested or complex structured data.

Partner with Us for Success

Experience seamless collaboration and exceptional results.

Updating JSON columns in ROR applications has many benefits, but there are some challenges that developers must successfully overcome. We'll look at some of the typical problems that arise while modifying JSON columns in Ruby on Rails apps in this blog post, along with solutions.

Challenges in updating JSON columns

JSON columns are convenient and flexible, and their structure makes it difficult to update them in Rails applications. When modifying JSON columns, developers frequently run into the following issues:

Nested Data:  JSON columns frequently have nested data which makes it difficult to update individual nested attributes without erasing the entire JSON object. Also, we have to ensure that updates are applied on the correct key. Here is sample JSON column data.

Concurrency: Simultaneous updates to the same JSON column can lead to concurrency issues.

Validation: It can be difficult to validate JSON data, particularly when conditional validation rules and hierarchical structures are involved.

In the above example, the User’s email should be present and unique. The validation makes sure that the name of each User record is distinct from all other User entries and is not empty.

In the above example, the profile is a JSON column. Because of the nested nature of JSON data, it may be difficult to add validation to individual keys within JSON data. If we want to apply presence validation on a specific key of JSON data, then we have to write custom validation for it.

Also applying uniqueness validation would be custom only, as first, we have retrieved it and compared it with the rest of the JSON data.

Querying and Indexing: If we compared our normal relational columns, JSON columns in the database might not be as effective for querying and indexing. To increase performance, you might need to carefully optimize your queries and carefully think about creating appropriate indexes for JSON columns.

In ROR we can add indexes to our columns using migration.

In the above example, add_index or CREATE INDEX is used to create an index on a profile column for a specific key key_to_index within the JSON data, using the ->> operator to fetch the value as a string, and the using::gin option specifies that the Generalized Inverted Index (GIN) method should be used, which is capable for indexing JSON columns in PostgreSQL.

Partner with Us for Success

Experience seamless collaboration and exceptional results.

Partial update: Rails has functionality for updating specific columns. It has activerecord methods such as update_column or update_columns. But It doesn't provide us with a specific nested key attribute to update. Every time updating an entire JSON column updates the whole JSON document, which is very inefficient and makes complex database operations. Rails retrieves the entire JSON document from the database, modifies it in memory, and then stores the entire document back into the database. Also, there is no built-in support at the database level for partially updating keys in certain JSON documents, resulting in inefficient data transfers.

Strategies for Overcoming Hurdles

It can be difficult to validate JSON data, particularly when conditional validation rules and hierarchical structures are involved.

Use of ActiveRecord methods

Rails has activerecords methods like update_column & update_columns for updating specific columns without triggering callbacks or validations. It helps to update specific attributes. It will be helpful when JSON data is smaller.

But when we talk about updating specific JSON columns, we will face a similar issue that we discussed above.

Utilizing Database function

When it comes to changing JSON columns in Rails, utilizing database functions usually means modifying JSON data in the database using SQL commands or certain functions offered by the DBMS instead of depending only on Rails ActiveRecord methods. This method is especially helpful in situations when it's important to optimize efficiency or when the changes require intricate manipulations of nested JSON structures.

Let's take an example: In a Rails application, we have a user_courses table containing a JSON column named progress storing user progress related to course information. Let's consider we want to update a specific attribute within the progress column for a user_course with a given content_id. Instead of fetching the entire JSON data, modifying it in rails code, and then saving it back to the DB, we can use database functions to perform the update directly in SQL.

Here's an example of using the MYSQL JSON_SET function to update a nested attribute within the JSON column.

Here's an example of using PostgreSQL's jsonb_set function to update a nested attribute within the JSON column.

In the above examples

  • UserCourses represents a table with a column named progress.
  • JSON_SET(data, '{$.nested_key}', '#{new_value}') is the MYSQL function that sets the value of the specified nested key within the data JSON column to the new_value.
  • JSONB_SET(data, '{nested_key}', '#{new_value}') is the PostgreSQL function that sets the value of the specified nested key within the data JSON column to the new_value.

Let's take the example of a nested attribute:

Existing progress column

Query Execution

After query execution

In the above example, we are updating the "status" and "progress_in_sec" within the specific key "_148". '$._148.status' '$._148.progress_in_sec' are representing the same. 

Better performance: Database operations are optimized for data manipulation resulting in faster updates compared to fetching and updating JSON data in a Rails application.

Less data transfer: Since updates are done directly at the database level, large JSON objects do not need to be transferred between the application and the database, reducing network overhead.

Atomicity and consistency: Database operations ensure that updates are performed atomically in a single transaction, maintaining data consistency and integrity.

This function is useful when you want to update individual attributes of a JSON column without overwriting the entire JSON document.

So, using database queries to update JSON columns in Rails applications provides a more efficient and better approach, especially for scenarios involving complex JSON data.

Conclusion:

Working with JSON columns in Ruby on Rails (ROR) can be tricky when it comes to updating them. Dealing with nested data, concurrency, and validation issues can be a headache.

To make life easier, developers have options. They can use basic Rails methods for simple updates, but for more precise changes, tapping into database functions like JSON_SET in MySQL or jsonb_set in PostgreSQL is the way to go.

This smarter approach speeds things up, cuts down on data back and forth, and ensures that updates happen smoothly and reliably. 

So, when dealing with complex JSON data in ROR apps, taking advantage of these database functions is definitely the way forward.

At F22 Labs, our team of expert Ruby on Rails developers specializes in tackling complex challenges like JSON column updates. With years of experience in building and optimizing Rails applications, we understand the intricacies of working with JSON data and can help streamline your development process. Hire our Ruby on Rails developers today and experience the F22 Labs difference in your next project.

Author-Piyush Duragkar
Piyush Duragkar

Backend Developer with 3+years of experience with Ruby on Rails. Specialising in backend optimisation, API development, and database. Enthusiastic about developing scalable, effective solutions.

Phone

Next for you

How to Integrate Google Maps into Your Flutter App Cover

Technology

Nov 20, 20244 min read

How to Integrate Google Maps into Your Flutter App

In our smartphone-driven world, location services are transforming the app landscape. The global location-based services market is projected to hit a staggering reach USD 129.71 billion by 2032, exhibiting a CAGR of 19.5% during the forecast period (2024-2032) This explosion stems from our smartphone addiction and hunger for personalized experiences. It's only natural that developers are rushing to integrate Google Maps into their apps, aiming to deliver an engaging, user-friendly mapping experi

How To Implement Dark Mode in Your Flutter App Cover

Technology

Oct 24, 20244 min read

How To Implement Dark Mode in Your Flutter App

In today's app-driven world, dark mode has become a must-have feature. A 2021 Android Authority survey found that an overwhelming 81.9% of users prefer dark mode in their apps. This trend highlights the need for Flutter developers to incorporate this feature to enhance user satisfaction and stay competitive. Understanding Theming in Flutter Flutter theming is the foundation for implementing dark mode. It provides a systematic way to manage your app's visual elements, ensuring consistency and

How to Implement Drag-and-Drop in Flutter? Cover

Technology

Oct 22, 20245 min read

How to Implement Drag-and-Drop in Flutter?

Have you ever marvelled at how smoothly you can rearrange apps on your smartphone's home screen? That's the magic of drag-and-drop at work. As a Flutter developer, you have the power to bring this same intuitive interaction to your apps. Let's dive into the world of Flutter drag and drop and discover how you can create interfaces that users will love to touch, drag, and interact with. Why Drag-and-Drop Matters in Modern Apps Picture this: you're scrolling through a to-do list app, and instea