You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cli.rst
+14-1Lines changed: 14 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -176,6 +176,19 @@ You can use the ``--json-cols`` option to automatically detect these JSON column
176
176
}
177
177
]
178
178
179
+
.. _cli_use_json_converters:
180
+
181
+
Automatic JSON deserialization
182
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
183
+
184
+
You can use the ``--use-json-converters`` flag to automatically deserialize columns that are declared as ``JSON`` (or inferred as such during insertion).
185
+
186
+
.. code-block:: bash
187
+
188
+
sqlite-utils query dogs.db "select * from dogs" --use-json-converters
189
+
190
+
If you use this flag with ``insert``, ``upsert`` or ``bulk``, it will also cause nested Python dictionaries or lists to be stored in columns with a declared type of ``JSON`` rather than ``TEXT``.
191
+
179
192
.. _cli_query_csv:
180
193
181
194
Returning CSV or TSV
@@ -1935,7 +1948,7 @@ Most of the time creating tables by inserting example data is the quickest appro
1935
1948
1936
1949
This will create a table called ``mytable`` with two columns - an integer ``id`` column and a text ``name`` column. It will set the ``id`` column to be the primary key.
1937
1950
1938
-
You can pass as many column-name column-type pairs as you like. Valid types are ``integer``, ``text``, ``float``and ``blob``.
1951
+
You can pass as many column-name column-type pairs as you like. Valid types are ``integer``, ``text``, ``float``, ``blob`` and ``json``.
1939
1952
1940
1953
Pass ``--pk`` more than once for a compound primary key that covers multiple columns.
Copy file name to clipboardExpand all lines: docs/python-api.rst
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -117,6 +117,14 @@ By default, any :ref:`sqlite-utils plugins <plugins>` that implement the :ref:`p
117
117
118
118
db = Database(memory=True, execute_plugins=False)
119
119
120
+
You can pass ``use_json_converters=True`` to enable automatic JSON conversion for columns declared as ``JSON``. This will register a custom converter with SQLite that uses ``json.loads()`` to deserialize values:
121
+
122
+
.. code-block:: python
123
+
124
+
db = Database("my_database.db", use_json_converters=True)
125
+
126
+
When this is enabled, Python ``dict``, ``list`` and ``tuple`` values will be stored in columns with a declared type of ``JSON``, and those columns will be automatically deserialized back into Python objects when you retrieve them from the database.
127
+
120
128
You can pass ``strict=True`` to enable `SQLite STRICT mode <https://www.sqlite.org/stricttables.html>`__ for all tables created using this database object:
0 commit comments