mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
Nicer error message for invalid JSON insert, closes #206
This commit is contained in:
parent
6d1828e40b
commit
1e38a16ea8
2 changed files with 26 additions and 5 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue