# Filter and sort on properties stored in a JSON column

> Use autoqueryjson() to let a grid or a REST client filter, sort and page over the properties of a JSON or JSONB column, with an allowlist and without writing any SQL.

Framework: total5
Product: Total.js 5 (total5@0.0.20)
Language: en
Content-SHA256: 6631ebb2866dad1cc436f6a7c15c785b0261b22a1d320a877dbc40b1228add2f
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

`autoquery()` turns the query string of a grid into filters for columns. When the data lives in **one JSON (or JSONB) column**, `0.0.20` has the same for its properties: `autoqueryjson($, column, schema, defsort, maxlimit)`. The `schema` is the allowlist, a comma separated list of `property` or `property:Type` (the type defaults to `string`), so a client can never filter, sort or read a property that you did not declare.

The builder does not write SQL. It names each field in a neutral way, `json:column:property:type`, in `fields`, in the filters and in `sort`, and the **database adapter** (PostgreSQL, MySQL, SQLite...) translates that name into its own syntax. That keeps the action free of any dialect. The column name is checked, and an invalid one throws.

The example driver only echoes the options that it receives, which is what an adapter reads.

```js definitions/driver.js
NEWDB('echo', function(options, callback) {
	callback(null, options);
});
```

```js controllers/people.js
exports.install = function() {
	ROUTE('GET /people/', people);
};

const SCHEMA = 'id,name,email,age:Number,dtcreated:Date,tags:[string]';

async function people($) {
	try {
		const options = await DB().list('echo/people').autoqueryjson($, 'data', SCHEMA, 'dtcreated_desc', 50).promise();
		$.json(options);
	} catch (e) {
		$.invalid(e);
	}
}
```

The query string uses the names of the properties: `?fields=id,name` chooses what to read, `?age=>18` filters, `?sort=name_desc` sorts and `?page=2&limit=500` pages (the limit never goes above `maxlimit`). Without `fields`, every property of the schema is read.
