Skip to content

Commit 7427a91

Browse files
committed
Output [] in JSON mode if no rows, closes #328
1 parent 77c240d commit 7427a91

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

sqlite_utils/cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,6 +2225,9 @@ def output_rows(iterator, headers, nl, arrays, json_cols):
22252225
)
22262226
yield line
22272227
first = False
2228+
if first:
2229+
# We didn't output any rows, so yield the empty list
2230+
yield "[]"
22282231

22292232

22302233
def maybe_json(value):

tests/test_cli.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,13 @@ def test_query_json(db_path, sql, args, expected):
994994
assert expected == result.output.strip()
995995

996996

997+
def test_query_json_empty(db_path):
998+
result = CliRunner().invoke(
999+
cli.cli, [db_path, "select * from sqlite_master where 0"]
1000+
)
1001+
assert result.output.strip() == "[]"
1002+
1003+
9971004
LOREM_IPSUM_COMPRESSED = (
9981005
b"x\x9c\xed\xd1\xcdq\x03!\x0c\x05\xe0\xbb\xabP\x01\x1eW\x91\xdc|M\x01\n\xc8\x8e"
9991006
b"f\xf83H\x1e\x97\x1f\x91M\x8e\xe9\xe0\xdd\x96\x05\x84\xf4\xbek\x9fRI\xc7\xf2J"
@@ -2098,7 +2105,11 @@ def test_indexes(tmpdir):
20982105

20992106
@pytest.mark.parametrize(
21002107
"extra_args,expected",
2101-
[([], _TRIGGERS_EXPECTED), (["articles"], _TRIGGERS_EXPECTED), (["counter"], "")],
2108+
[
2109+
([], _TRIGGERS_EXPECTED),
2110+
(["articles"], _TRIGGERS_EXPECTED),
2111+
(["counter"], "[]\n"),
2112+
],
21022113
)
21032114
def test_triggers(tmpdir, extra_args, expected):
21042115
db_path = str(tmpdir / "test.db")

0 commit comments

Comments
 (0)