mirror of
https://github.com/simonw/datasette.git
synced 2026-06-14 21:16:56 +02:00
feat: add a wildcard for _json columns
This commit is contained in:
parent
2e9751672d
commit
1cdcd8894c
2 changed files with 8 additions and 2 deletions
|
|
@ -10,13 +10,13 @@ from datasette.utils.asgi import Response
|
||||||
|
|
||||||
def convert_specific_columns_to_json(rows, columns, json_cols):
|
def convert_specific_columns_to_json(rows, columns, json_cols):
|
||||||
json_cols = set(json_cols)
|
json_cols = set(json_cols)
|
||||||
if not json_cols.intersection(columns):
|
if not json_cols.intersection(columns) and not json_cols == {"*"}:
|
||||||
return rows
|
return rows
|
||||||
new_rows = []
|
new_rows = []
|
||||||
for row in rows:
|
for row in rows:
|
||||||
new_row = []
|
new_row = []
|
||||||
for value, column in zip(row, columns):
|
for value, column in zip(row, columns):
|
||||||
if column in json_cols:
|
if column in json_cols or (json_cols == {"*"}):
|
||||||
try:
|
try:
|
||||||
value = json.loads(value)
|
value = json.loads(value)
|
||||||
except (TypeError, ValueError) as e:
|
except (TypeError, ValueError) as e:
|
||||||
|
|
|
||||||
|
|
@ -190,6 +190,12 @@ query string arguments:
|
||||||
JSON. Without this argument those columns will be returned as JSON objects
|
JSON. Without this argument those columns will be returned as JSON objects
|
||||||
that have been double-encoded into a JSON string value.
|
that have been double-encoded into a JSON string value.
|
||||||
|
|
||||||
|
If you have many columns containing JSON values, you can pass ``_json=*``
|
||||||
|
to attempt converting loading all columns as JSON. This is a best effort
|
||||||
|
approach. Any columns that fail to render as valid JSON will be passed
|
||||||
|
through unaltered, while any column with valid JSON will be converted.
|
||||||
|
There may be a performance impact on large result sets.
|
||||||
|
|
||||||
Compare `this query without the argument <https://fivethirtyeight.datasettes.com/fivethirtyeight.json?sql=select+%27{%22this+is%22%3A+%22a+json+object%22}%27+as+d&_shape=array>`_ to `this query using the argument <https://fivethirtyeight.datasettes.com/fivethirtyeight.json?sql=select+%27{%22this+is%22%3A+%22a+json+object%22}%27+as+d&_shape=array&_json=d>`_
|
Compare `this query without the argument <https://fivethirtyeight.datasettes.com/fivethirtyeight.json?sql=select+%27{%22this+is%22%3A+%22a+json+object%22}%27+as+d&_shape=array>`_ to `this query using the argument <https://fivethirtyeight.datasettes.com/fivethirtyeight.json?sql=select+%27{%22this+is%22%3A+%22a+json+object%22}%27+as+d&_shape=array&_json=d>`_
|
||||||
|
|
||||||
``?_json_infinity=on``
|
``?_json_infinity=on``
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue