diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 5f81a1e..4d888ef 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -654,12 +654,19 @@ def insert_upsert_implementation( reader = csv_std.reader(json_file, dialect=dialect) headers = next(reader) docs = (dict(zip(headers, row)) for row in reader) - elif nl: - docs = (json.loads(line) for line in json_file) else: - docs = json.load(json_file) - if isinstance(docs, dict): - docs = [docs] + try: + if nl: + docs = (json.loads(line) for line in json_file) + else: + docs = json.load(json_file) + if isinstance(docs, dict): + docs = [docs] + except json.decoder.JSONDecodeError: + raise click.ClickException( + "Invalid JSON - use --csv for CSV or --tsv for TSV files" + ) + extra_kwargs = {"ignore": ignore, "replace": replace, "truncate": truncate} if not_null: extra_kwargs["not_null"] = set(not_null) diff --git a/tests/test_cli.py b/tests/test_cli.py index 59770c3..381997b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -584,6 +584,20 @@ def test_insert_from_stdin(tmpdir): ) +def test_insert_invalid_json_error(tmpdir): + db_path = str(tmpdir / "dogs.db") + result = CliRunner().invoke( + cli.cli, + ["insert", db_path, "dogs", "-"], + input="name,age\nCleo,4", + ) + assert result.exit_code == 1 + assert ( + result.output + == "Error: Invalid JSON - use --csv for CSV or --tsv for TSV files\n" + ) + + def test_insert_with_primary_key(db_path, tmpdir): json_path = str(tmpdir / "dog.json") open(json_path, "w").write(json.dumps({"id": 1, "name": "Cleo", "age": 4}))