From 8631205a620c70b2979052c81dc536994f6eb5ef Mon Sep 17 00:00:00 2001 From: leo7martins <219400372+leo7martins@users.noreply.github.com> Date: Sat, 21 Nov 2026 18:15:46 +0000 Subject: [PATCH] Add a --json option for batch output as JSON Lines (#263) Batch mode (-e and stdin) can now emit results as JSON Lines, one JSON object per row, via cli_helpers' jsonl adapter: litecli db.sqlite -e "select * from users" --json --csv still takes precedence if both flags are given, and the default (tsv when no format flag is passed) is unchanged. Also documents jsonl as a valid table_format value in liteclirc. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- CHANGELOG.md | 4 ++++ litecli/liteclirc | 2 +- litecli/main.py | 8 ++++++++ tests/test_main.py | 31 +++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d91317..1f205c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Unreleased +### Features + +- Add a `--json` option for batch query output (JSON Lines, one object per row), e.g. `litecli -e "select ..." --json`. (#263) + ### Bug Fixes - Expand `~` in configured log file paths before opening the log. diff --git a/litecli/liteclirc b/litecli/liteclirc index ec162d3..0b74a79 100644 --- a/litecli/liteclirc +++ b/litecli/liteclirc @@ -39,7 +39,7 @@ log_level = INFO # Table format. Possible values: # ascii, double, github, psql, plain, simple, grid, fancy_grid, pipe, orgtbl, # rst, mediawiki, html, latex, latex_booktabs, textile, moinmoin, jira, -# vertical, tsv, csv. +# vertical, tsv, csv, jsonl. # Recommended: ascii table_format = ascii diff --git a/litecli/main.py b/litecli/main.py index fa732c3..fb713db 100644 --- a/litecli/main.py +++ b/litecli/main.py @@ -931,6 +931,7 @@ def get_last_query(self) -> str | None: ) @click.option("-t", "--table", is_flag=True, help="Display batch output in table format.") @click.option("--csv", is_flag=True, help="Display batch output in CSV format.") +@click.option("--json", is_flag=True, help="Display batch output in JSON format (one JSON object per line).") @click.option("--warn/--no-warn", default=None, help="Warn before running a destructive query.") @click.option("-e", "--execute", type=str, help="Execute command and quit.") @click.argument("database", default="", nargs=1) @@ -942,6 +943,7 @@ def cli( auto_vertical_output: bool, table: bool, csv: bool, + json: bool, warn: bool | None, execute: str | None, liteclirc: str, @@ -973,6 +975,9 @@ def cli( try: if csv: litecli.formatter.format_name = "csv" + elif json: + # cli_helpers' JSON Lines adapter: one JSON object per row. + litecli.formatter.format_name = "jsonl" elif not table: litecli.formatter.format_name = "tsv" @@ -1000,6 +1005,9 @@ def cli( if csv: litecli.formatter.format_name = "csv" + elif json: + # cli_helpers' JSON Lines adapter: one JSON object per row. + litecli.formatter.format_name = "jsonl" elif not table: litecli.formatter.format_name = "tsv" diff --git a/tests/test_main.py b/tests/test_main.py index 21ad5b6..09f49aa 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -76,6 +76,20 @@ def test_execute_arg_with_csv(executor): assert expected in "".join(result.output) +@dbtest +def test_execute_arg_with_json(executor): + run(executor, "create table test (a text)") + run(executor, 'insert into test values("abc")') + + sql = "select * from test;" + runner = CliRunner() + result = runner.invoke(cli, args=CLI_ARGS + ["-e", sql] + ["--json"]) + expected = '{"a":"abc"}\n' + + assert result.exit_code == 0 + assert expected in "".join(result.output) + + @dbtest def test_batch_mode(executor): run(executor, """create table test(a text)""") @@ -134,6 +148,23 @@ def test_batch_mode_csv(executor): assert expected in "".join(result.output) +@dbtest +def test_batch_mode_json(executor): + run(executor, """create table test(a text, b text)""") + run(executor, """insert into test (a, b) values('abc', 'de\nf'), ('ghi', 'jkl')""") + + sql = "select * from test;" + + runner = CliRunner() + result = runner.invoke(cli, args=CLI_ARGS + ["--json"], input=sql) + + # JSON Lines: one JSON object per row, embedded newlines escaped. + expected = '{"a":"abc","b":"de\\nf"}\n{"a":"ghi","b":"jkl"}\n' + + assert result.exit_code == 0 + assert expected in "".join(result.output) + + def test_help_strings_end_with_periods(): """Make sure click options have help text that end with a period.""" for param in cli.params: