Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic JSON de-serialization #612

Open
rsyring opened this issue Jan 22, 2024 · 0 comments
Open

Automatic JSON de-serialization #612

rsyring opened this issue Jan 22, 2024 · 0 comments

Comments

@rsyring
Copy link

rsyring commented Jan 22, 2024

My use case is that I'd like to more easily import and explore JSON documents returned from APIs. Obviously, the automatic type detection and table creation provided by sqlite-utils (SU) adds considerable productivity to that effort.

One of the difficulties I've encountered is that SU will serialize dicts/lists to JSON but doesn't deserialize it when retrieved. It turns out that with a bit of modification, it would not be hard to support this use case. This is what I came up with:

import json
import sqlite3

from sqlite_utils import db


db.COLUMN_TYPE_MAPPING['JSON'] = 'JSONB'


class Database(db.Database):
    def __init__(self, fpath, *args, **kwargs):
        conn = sqlite3.connect(fpath, detect_types=sqlite3.PARSE_DECLTYPES)
        sqlite3.register_converter('jsonb', self.json_loads)

        super().__init__(conn, *args, **kwargs)

    # Keep this on the class so it can be easily customized in a subclass
    def json_loads(self, val: bytes):
        return json.loads(val)


db = Database(':memory:')

db['users'].insert(
    {'id': 1, 'name': 'John Doe', 'preferences': {'theme': 'dark', 'language': 'en'}},
    columns={'preferences': 'JSON'},
    replace=True,
)

row = db['users'].get(1)
assert isinstance(row['preferences'], dict), row

This could all be made mostly automatic if SU:

  1. Column type detection used 'JSON' instead of 'TEXT' when detecting Python objects that will be jsonified
  2. sqlite3 is setup to handle JSON/JSONB conversion

Considerations:

  1. I'd like to see this made the default but until the next major version bump, could be hidden behind Database(..., jsonb_columns=True)
  2. Whether or not JSON or JSONB columns are used would depend on SQLite version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant