skip to content

As a team default for Room entities, when should a list such as an event's attendee ids be a converter-backed JSON column rather than its own table?

level: principalimportance: should knowfreq 38%

answer

  1. what Room can still check
  2. querying by one element
  3. foreign keys stop at the column
  4. a format outside the exported schema

basics

~20 s

Use a converter-backed JSON column only for small, bounded values read and written whole and never queried by element. Anything queried, referenced or growing belongs in a table, where Room's indexes, foreign keys, relations and exported schema still apply.

solid answer

~50 s

A converter hides the list's structure from Room: compile-time query checks, `@Index`, `@ForeignKey` and relations see one TEXT column, and the exported schema records only that column, so the JSON shape becomes an unversioned second schema. Querying "events user 42 attends" then needs `LIKE` or SQLite's JSON functions with no ordinary index, deleting a user leaves stale ids inside the text, and each change rewrites the whole value. My default is a cross-reference table with a composite key, foreign keys and an index on the user id, read through a relation. A JSON column is fine for a bounded, opaque value that is always read whole, like a cached server snapshot, with one shared converter registered once and a tolerant decoder. Moving from JSON to a table later costs a data migration on every phone.

go deeper

for a junior

Know that Room can store a list through a converter that writes text, and that a separate table is the other option.

for a middle

Explain which Room features stop at a converter column: indexes, foreign keys, relations and the exported schema's view of the data.

for a senior

Describe the migration that turns an existing JSON column into a table, and why Room cannot generate it.

for a principal

Set the default and its exceptions: tables for referenced or queried collections, converters for bounded opaque values, with the format owned and reviewed.

## Two ways to keep a list on an entity An events app stores, for each event, the ids of the users attending it. With Room there are two shapes: - a **converter-backed column**: `val attendeeIds: List<Long>` on the `Event` entity, with a `@ColumnTypeConverter` pair (Room 2.x: `@TypeConverter`) that writes the list as JSON text and parses it back; - a **separate table**: an `EventAttendee(eventId, userId)` entity with a composite primary key, foreign keys to both tables, and an index on `userId`, read back with a relation or a join. The converter version is a few lines and reads the whole list in one row. The question a lead has to answer is when that convenience is worth what Room stops seeing. ## What Room no longer sees through a converter Room's value is that it **checks** your data model at compile time and **tracks** it across versions. A converter ends both at the column boundary: | Room feature | Separate table | JSON column via converter | |---|---|---| | compile-time SQL checks | check every column you filter on | see one TEXT column, not its elements | | `@Index` | can index `userId` | cannot index an element inside the text | | `@ForeignKey` with `onDelete` | keeps attendees consistent with users | cannot reach inside the value | | relations (`@Relation`, `@Junction`) | load attendees with the event | not applicable | | exported schema and auto-migrations | record each column's type | record one TEXT column; the JSON shape is invisible | | invalidation of observable queries | per table | per table, the same | The last row is a wash: Room invalidates by table either way, so neither shape makes reactive queries cheaper. The other rows all favour the table whenever the elements matter to a query. ## The costs of the JSON column 1. **Querying by element.** "Which events does user 42 attend?" needs `LIKE` matching or SQLite's JSON functions (covered in the SQLite lessons), with no ordinary index to help. 2. **Integrity.** Deleting a user leaves its id inside every JSON value that mentions it; only app code can clean that up. 3. **Write amplification.** Adding one attendee rewrites the whole value. 4. **An unversioned second schema.** The element format lives in the converter, outside Room's exported schema, so a format change needs a hand-written migration that Room cannot verify. 5. **Decode cost.** Every read of the row runs the converter, on whatever context runs the query. ## When the JSON column is the right call A converter-backed value is a sound choice when it is genuinely **one value**: - it is small and bounded, and always read and written whole; - nothing queries, sorts or joins by its elements; - nothing else in the database refers to its elements; - it mirrors a payload the app displays as is, such as a cached server snapshot. ## A team default worth enforcing A defensible policy for a team using Room: - **Default to a table** for anything that is a collection of references, grows without bound, or appears in a `WHERE`, `ORDER BY` or join. - **Allow a converter column** for bounded, opaque values, and require the reason in code review. - **Make the format explicit**: one shared converter, registered once, with a tolerant decoder; when it needs a configured JSON instance, supply it as a provided converter (`@ProvidedColumnTypeConverter`). - **Plan the exit**: moving from a JSON column to a table later means a migration that parses every stored value on users' phones, which costs far more than starting with the table. The general question of storing server responses whole or normalised belongs to mobile system design; the Room-specific point is that a converter trades away Room's own checks for that column.

  • If you inherit a JSON attendee column and need to query by user, what are your options?
    Short term, filter in SQL with SQLite's JSON functions or in app code after loading, accepting a scan. Properly, add an attendee table in a new schema version and write a migration that parses each stored value into rows; Room cannot generate that migration because it never knew the JSON's shape.
  • Does a separate table make a Flow of events re-emit less often than a JSON column?
    Not by itself. Room invalidates observable queries by table, so a query reading both events and attendees re-emits when either table changes. The table shape wins on queryability, integrity and indexing, not on how often observable queries fire.

  • Searching JSON text with LIKE is as good as an indexed lookup
  • Room's foreign keys also protect ids stored inside a converter column
  • Room's exported schema records the structure of a converted list
  • Converter columns make observable queries re-emit less often
  • Normalising a list is always wrong for mobile because of joins

Converter names are Room 3.0's (@ColumnTypeConverter, @ProvidedColumnTypeConverter); in Room 2.8.5 they are @TypeConverter and @ProvidedTypeConverter, and the trade-off is the same.