Add a new attribue type to CubicWeb: Jsonb. This type is mapped to the jsonb PostgreSQL data type.
Declaration
In your schemas, you can use the new type as follow:
>>> class MyEntityType(EntityType): ... json_attribute = Jsonb(required=True) ...
Get/Set
To provide a value for a Jsonb attribute, you can use either:
a dict,
a JSON string.
For example, you can write the following code:
>>> my_entity.cw_set(json_attribute={'a': [1, 2, 3]})
The code below wil have exactly the same effect:
>>> my_entity.cw_set(json_attribute=u'{"a": [1, 2, 3]}')
Please note that, whatever way you set the value (string or dict), you will always get back a dict when asking for it:
>>> my_entity.json_attribute
{u'a': [1, 2, 3]}
Querying
In RQL, you can query a Jsonb attribute in multiple ways. For example, you can ask for attributes containing a specific key/value pair:
>>> import json
>>> rql('Any X WHERE X json_attribute J HAVING JSONB_CONTAINS(J, %(json_value)s)=True',
{'json_value': json.dumps({u'a': 1})})
You can ask for existence of a specific key:
>>> rql('Any X WHERE X json_attribute J HAVING JSONB_EXISTS(J, %(key)s)=True',
{'key': u'b'})
You can get the value for a key:
>>> rql('Any JSONB_GET(J, %(key)s) WHERE X json_attribute J, X eid %(eid)s',
{'key': u'a', 'eid': 1234})
Note: JSONB_GET() will always return a string. If the value is a JSON object (or a JSON array), you may want to use json.loads() afterwards to have a dict (or a list).
One final remark: as the PostgreSQL documentation suggests, you should use the same structure for your JSON data in the same column. This makes querying much easier.
Metadata
Release files for cubicweb-jsonb 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cubicweb-jsonb-0.4.0.tar.gz | 9.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cubicweb_jsonb-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.9 kB
Release files / cubicweb-jsonb-0.4.0.tar.gz
| Download URL | cubicweb-jsonb-0.4.0.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9593dfdf325d519632231ee06624f3bbbea86a5f0d44592c175ae26a0d5a51d1
|
|
BLAKE2b-256 checksum How to use checksums |
11c3097ccceaa9f524cabd6d35ea2ea1229e95145df4f74924d602f90532f943
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.10.2
|
Release files / cubicweb_jsonb-0.4.0-py3-none-any.whl
| Download URL | cubicweb_jsonb-0.4.0-py3-none-any.whl |
|---|---|
| Size | 7.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d46ddb78fc45e33441783c56d7da07bc718cfa5aefbc4765ea06a5d77ef577e6
|
|
BLAKE2b-256 checksum How to use checksums |
d66e10d8f358bbe824afb02057246d683193ac72d34fc0335a5374693a9cf5b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.10.2
|