Skip to content

Commit 12b8c9d

Browse files
committed
sqlite-utils memory --flatten, closes #332
1 parent 13195d8 commit 12b8c9d

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

docs/cli.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ This also means you pipe ``sqlite-utils`` together to easily create a new SQLite
758758
Flattening nested JSON objects
759759
------------------------------
760760

761-
``sqlite-utils insert`` expects incoming data to consist of an array of JSON objects, where the top-level keys of each object will become columns in the created database table.
761+
``sqlite-utils insert`` and ``sqlite-utils memory`` both expect incoming JSON data to consist of an array of JSON objects, where the top-level keys of each object will become columns in the created database table.
762762

763763
If your data is nested you can use the ``--flatten`` option to create columns that are derived from the nested data.
764764

sqlite_utils/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,7 @@ def query(
11901190
multiple=True,
11911191
help="Additional databases to attach - specify alias and filepath",
11921192
)
1193+
@click.option("--flatten", is_flag=True, help="Flatten nested JSON objects")
11931194
@output_options
11941195
@click.option("-r", "--raw", is_flag=True, help="Raw output, first column of first row")
11951196
@click.option(
@@ -1226,6 +1227,7 @@ def memory(
12261227
paths,
12271228
sql,
12281229
attach,
1230+
flatten,
12291231
nl,
12301232
arrays,
12311233
csv,
@@ -1300,6 +1302,8 @@ def memory(
13001302
if format_used in (Format.CSV, Format.TSV) and not no_detect_types:
13011303
tracker = TypeTracker()
13021304
rows = tracker.wrap(rows)
1305+
if flatten:
1306+
rows = (dict(_flatten(row)) for row in rows)
13031307
db[csv_table].insert_all(rows, alter=True)
13041308
if tracker is not None:
13051309
db[csv_table].transform(types=tracker.types)

tests/test_cli_memory.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,30 @@ def test_memory_no_detect_types(option):
222222
]
223223

224224

225+
def test_memory_flatten():
226+
result = CliRunner().invoke(
227+
cli.cli,
228+
["memory", "-", "select * from stdin", "--flatten"],
229+
input=json.dumps(
230+
{
231+
"httpRequest": {
232+
"latency": "0.112114537s",
233+
"requestMethod": "GET",
234+
},
235+
"insertId": "6111722f000b5b4c4d4071e2",
236+
}
237+
),
238+
)
239+
assert result.exit_code == 0, result.output
240+
assert json.loads(result.output.strip()) == [
241+
{
242+
"httpRequest_latency": "0.112114537s",
243+
"httpRequest_requestMethod": "GET",
244+
"insertId": "6111722f000b5b4c4d4071e2",
245+
}
246+
]
247+
248+
225249
def test_memory_analyze():
226250
result = CliRunner().invoke(
227251
cli.cli,

0 commit comments

Comments
 (0)