# Search inside Dict

**URL:** https://discuss.tryton.org/t/search-inside-dict/929
**Category:** Feature
**Created:** [December 3, 2018, 6:12pm UTC](https://discuss.tryton.org/t/search-inside-dict/929 "2018-12-03T18:12:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [December 3, 2018, 6:12pm UTC](https://discuss.tryton.org/t/search-inside-dict/929/1 "2018-12-03T18:12:38Z")

</div>

## Rational

We have a [Dict](http://docs.tryton.org/projects/server/en/latest/ref/models/fields.html#dict) but it is not really possible to use them in a domain because it does string comparison.  
It will be a good improvement to be able to make domain on the key value.

Both back-ends have function to manipulate JSON data: [PostgreSQL JSON function](https://www.postgresql.org/docs/current/functions-json.html) and [SQLite JSON1 extension](https://www.sqlite.org/json1.html).

For now, there is only Oracle which supports the [SQL/JSON standard](https://modern-sql.com/blog/2017-06/whats-new-in-sql-2016#json). So an had-hoc solution for the Tryton limited usage is preferable.

## Proposal

I propose to extend the domain syntax to allow the ‘.’ notation (like for relation field) to access the value of a specific key. For example `('dict.key', '=', value)` will search for record with the JSON object having _key_ value equals to _value_.  
This can be implemented in PostgreSQL with the operator `dict->>key = value` and in SQLite with `json_extract(dict, '$.key') = value`.

On the client side, the domain evaluation can be adapted to test the value of the key. The domain inversion can just ignore such case. And finally the domain parser could have a syntax for this but it is not mandatory (as it can be too expensive to retrieve all the key strings).

## Implementation

> **[Allow to search and order on Dict key value (#7905) · Issues · Tryton / Tryton...](https://foss.heptapod.net/tryton/tryton/-/issues/7905)**
>
> From https://discuss.tryton.org/t/search-inside-dict/929

---

<div class="post-metadata">

### Author: ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)
#### Post date: [December 5, 2018, 10:22pm UTC](https://discuss.tryton.org/t/search-inside-dict/929/2 "2018-12-05T22:22:56Z")

</div>

Whow, this is VERY nice! I’m using Tryton for storing data about sensors and systems. The “realtime” sensor data is stored on different intervals and to minimize extra rows, I’m storing the data of the different sensor into a Dict als “sensorname” → “value”.  
To get the values out of the database I had to write my own SQL-query, but this is becoming more Tryton like 🙂

\<“offtopic”\>  
The only thing missing now is getting the date from a datetime stamp in the database. E.g. you have a datetime column called “storedate” with the value 2018-12-04 23:00:10. In PostgreSQL you can get only the date by calling storedate::date or only the time bij calling storedate::time. But I think this is something for the python-sql module.  
\<“/offtopic”\>

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [February 1, 2019, 5:00pm UTC](https://discuss.tryton.org/t/search-inside-dict/929/3 "2019-02-01T17:00:06Z")

</div>

This topic was automatically closed after 14 days. New replies are no longer allowed.
