Skip to content

Instantly share code, notes, and snippets.

@efrence
Forked from iAnatoly/json-to-sqlite.md
Created December 24, 2022 19:06
Show Gist options
  • Save efrence/275efa25c441106ad107a1148428be8b to your computer and use it in GitHub Desktop.
Save efrence/275efa25c441106ad107a1148428be8b to your computer and use it in GitHub Desktop.
Convert json to a sqlite database for querying

Sometimes, you need to quickly dump a json file into sqlite, just to run some queries on it. This is a two-step process:

1. convert json to csv

cat file.json | jq -r '.data | map([.field1, .field2, .field3] | @csv)| join("\n")' > file.csv

1a. add headers line into your csv file, i.e.

ex -sc '1i|field1,field2,field3' -cwq file.csv

2. import csv into sqlite

$ sqlite3 file.sqlite
SQLite version 3.24.0 2018-06-04 14:10:15
Enter ".help" for usage hints.
sqlite> .mode csv
sqlite> .import file.csv data
sqlite> .tables
data
sqlite> .schema dat
CREATE TABLE incidents(
  "field1" TEXT,
  "field2" TEXT,
  "field3" TEXT
);
sqlite> select count(*), field3 from data group by field3;
3,test3
1,test1
sqlite>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment