Skip to main content

JSON

Ormed supports:

  • JSON fields on models via casts / codecs
  • JSON path queries via whereJson* helpers
  • JSON selector syntax using -> / ->> with $.path expressions
  • JSON updates (set / patch) via query updates or tracked model helpers
note

These examples focus on JSON operations only. They assume you already have a working DataSource.

Model fields

(table: 'users')
class User extends Model<User> {
const User({required this.id, required this.email, this.metadata});

(isPrimaryKey: true, autoIncrement: true)
final int id;

final String email;

// Use the built-in `json` cast key for JSON objects.
(cast: 'json')
final Map<String, Object?>? metadata;
}

Query JSON

In Dart strings, $ is interpolation — if you write selectors with $.path, use raw strings (r'...'), or omit the $ prefix (metadata->>mode).

Array indexes can be addressed as either:

  • items[0] (explicit), or
  • items.0 (shorthand; normalized to items[0])

If you need a numeric object key, use bracket quoting: items["0"].

Future<List<User>> jsonWhereHelpers(DataSource dataSource) {
return dataSource
.query<$User>()
.whereJsonContains('metadata', 'Dart', path: r'$.skills')
.whereJsonContainsKey('metadata', r'$.flags')
.whereJsonLength('metadata->tags', '>=', 2)
.whereJsonOverlaps('metadata->skills', ['Dart', 'Go'])
.get();
}

Update JSON

Future<void> jsonUpdateQuery(DataSource dataSource) async {
await dataSource.query<$User>().whereEquals('id', 1).update(
{'email': 'updated@example.com'},
jsonUpdates: (_) => [
JsonUpdateDefinition.path('metadata', r'$.meta.count', 5),
JsonUpdateDefinition.selector('metadata->mode', 'light'),
// Update JSON array elements (both forms normalize the same way).
JsonUpdateDefinition.path(
'metadata',
r'$.meta.profile.items[1].label',
'b2',
),
JsonUpdateDefinition.path(
'metadata',
r'$.meta.profile.items.0.label',
'a2',
),
JsonUpdateDefinition.patch('metadata', {
'flags': {'beta': true},
}),
],
);
}