JSON to SQLite

· 153 words · 1 minute read

I have a script that queries multiple records from an external API and prints a new JSON string per record to stdout. I want a nice way to explore those results.

In the past, I’ve used jq to format records as CSV and import them into SQLite. Today, I learned about sqlite-utils from Datasette. It looks like it does lots of cool things, but for today, I can pipe a list of JSON objects to be inserted into a table on a database. If either the database or the table doesn’t exist already, they’ll be created.

With my current script, each record goes to stdout, not as a list, but that’s fixable by slurping records into jq. This works well, as all of these events are streamable:

  • print a record
  • slurp it into jq
  • import it to sqlite.
<script that streams json outputs> | jq -s | sqlite-utils insert my.db some_table -

…beautiful…