Skip to content

--key and --auto-key for importing JSON rows nested under a key - #995

Open
officialasishkumar wants to merge 1 commit into
simonw:mainfrom
officialasishkumar:json-key-489
Open

officialasishkumar wants to merge 1 commit into
simonw:mainfrom
officialasishkumar:json-key-489

Conversation

@officialasishkumar

Copy link
Copy Markdown

Closes #489

JSON exports often wrap their records in an outer object, which currently has to go through jq .List before sqlite-utils can import it:

{"Version": "5.5.52.6", "List": [{"Id": 1, "Description": "Nonpartisan"}, {"Id": 2, "Description": "Undeclared"}]}

This implements the --key / --auto-key design from the issue:

sqlite-utils insert data.db parties parties.json --key List --pk Id
sqlite-utils insert data.db parties parties.json --auto-key --pk Id
sqlite-utils memory parties.json "select * from parties" --key List

Behaviour

  • --key X reads rows from key X of the top-level object. A single object under the key becomes one row, as suggested in the issue. A missing key, a top-level list, or a key holding a scalar value produces a clear error rather than a KeyError.
  • --auto-key uses the only key holding a non-empty list of objects. If there is no such key, or more than one, the JSON is imported exactly as it would be without the option, so it is safe to use across a mix of files with memory.
  • Both options are available on insert, upsert and bulk (through the shared _import_options) and on memory. insert/upsert/bulk reject them alongside --nl, --csv, --tsv, --lines, --text and --code. memory ignores them for CSV, TSV and newline-delimited JSON files, the same way rows_from_file() treats its other format-specific options.
  • rows_from_file() gains matching key= and auto_key= parameters, documented in its docstring.

The issue suggested refactoring insert to use rows_from_file() as part of this work. I left that out to keep the change focused: insert keeps its existing streaming reader, and the key lookup lives in a small _select_json_rows() helper shared by both code paths, so their behaviour can't drift apart.

memory now reports RowsFromFileBadJSON as a clean Error: message. As a side effect, JSON that is neither a list nor an object now gets that message instead of a traceback.

Docs

There's a new "Inserting JSON data nested under a key" section in docs/cli.rst, with a pointer to it from the memory docs. cli-reference.rst is regenerated with cog.

Tests

New tests in test_rows_from_file.py, test_cli_insert.py (insert and upsert), test_cli_memory.py and test_cli_bulk.py cover both options, the --auto-key fallbacks, the error messages and the options being ignored for other formats. They fail on main and pass with this change.

Locally (Python 3.13, SQLite 3.54) the full suite passes (1668 passed, 17 skipped), and so do black, flake8, mypy, pyright, ty, cog, codespell and a sphinx-build -W docs build.

simonw#489

JSON exports often wrap their records in an outer object, for example
{"Version": "...", "List": [{...}, {...}]}. Until now these had to be
piped through "jq .List" before sqlite-utils could import them.

rows_from_file() gains two parameters for JSON data:

- key="List" reads the rows from that key of the top-level object. A
  single object under the key is treated as one row. A missing key, a
  top-level value that is not an object, or a key holding something
  other than a list or object raises RowsFromFileBadJSON.
- auto_key=True reads the rows from the only key of the top-level object
  that contains a non-empty list of objects. If there is no such key, or
  more than one, the JSON is handled exactly as it is without the option.

Passing both raises ValueError. Like the other format-specific options,
they are ignored for CSV, TSV and newline-delimited JSON.

The CLI exposes these as --key and --auto-key on insert, upsert and bulk
(through the shared import options) and on memory. insert keeps its own
streaming reader, so the key lookup lives in a _select_json_rows() helper
that both code paths share. insert, upsert and bulk reject the options
alongside --nl, --csv, --tsv, --lines, --text or --code. Errors from the
key lookup, and passing both options at once, are reported as clean error
messages rather than tracebacks.

Documented in a new "Inserting JSON data nested under a key" section,
with a pointer from the memory documentation.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T20:37:18.473623Z 73f604d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to load JSON records held in a file with a single top level key that is a list of objects

1 participant