Repository navigation
--key and --auto-key for importing JSON rows nested under a key - #995
Open
officialasishkumar wants to merge 1 commit into
Open
officialasishkumar wants to merge 1 commit into
officialasishkumar wants to merge 1 commit into
Conversation
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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #489
JSON exports often wrap their records in an outer object, which currently has to go through
jq .Listbeforesqlite-utilscan import it:{"Version": "5.5.52.6", "List": [{"Id": 1, "Description": "Nonpartisan"}, {"Id": 2, "Description": "Undeclared"}]}This implements the
--key/--auto-keydesign 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 ListBehaviour
--key Xreads rows from keyXof 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 aKeyError.--auto-keyuses 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 withmemory.insert,upsertandbulk(through the shared_import_options) and onmemory.insert/upsert/bulkreject them alongside--nl,--csv,--tsv,--lines,--textand--code.memoryignores them for CSV, TSV and newline-delimited JSON files, the same wayrows_from_file()treats its other format-specific options.rows_from_file()gains matchingkey=andauto_key=parameters, documented in its docstring.The issue suggested refactoring
insertto userows_from_file()as part of this work. I left that out to keep the change focused:insertkeeps 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.memorynow reportsRowsFromFileBadJSONas a cleanError: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 thememorydocs.cli-reference.rstis regenerated with cog.Tests
New tests in
test_rows_from_file.py,test_cli_insert.py(insert and upsert),test_cli_memory.pyandtest_cli_bulk.pycover both options, the--auto-keyfallbacks, the error messages and the options being ignored for other formats. They fail onmainand 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 -Wdocs build.