Is there a way to convert the result of a SELECT
query on a table directly to JSON without writing it to a file? Perhaps with the JSON extension of duckdb
?
I could also use the python client, where I’d convert the result to a pandas dataframe and then to JSON, but I figured there should be a more direct way.
Example:
CREATE TABLE weather (
city VARCHAR,
temp_lo INTEGER, -- minimum temperature on a day
temp_hi INTEGER, -- maximum temperature on a day
prcp REAL,
date DATE
);
INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
INSERT INTO weather VALUES ('Vienna', -5, 35, 10, '2000-01-01');
An example query would be "SELECT city, temp_hi FROM weather;"
, and the desired json would look like:
{"city": ["San Francisco", "Vienna"], "temp_hi": [50, 35]}
So to recap, I’m looking for way to create the desired JSON directly, without converting the result to a python object first.
2
Answers
It is unclear where are you writing the query and where are you expecting the output of the query.
In case you are using the duckdb CLI tool, you can set "mode" before querying anything like this
If its using the Python client API, from check the source code, it appears it doesn’t have implemented a JSON mode like the CLI tool does. It does have a
render_mode
parameter but it only has 2 options. You can test it by writing a query and passing the render_mode to theshow()
function. likecon.sql("SELECT city, temp_hi form weather").show(render_mode=1)
. You can just use thefetchall()
which returns a tuple of the resultsAnyways if going with the Python route, why not use the conversion options? such as a pandas dataframe
You can use the
list
aggregate function with aSTRUCT
. The latter can be defined using curly braces orstruct_pack
:Both of them will produce the same result: