Skip to content

Instantly share code, notes, and snippets.

@drewcsillag
Created February 28, 2021 20:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewcsillag/1bbf146f30a11adfee02c911b6a98464 to your computer and use it in GitHub Desktop.
Save drewcsillag/1bbf146f30a11adfee02c911b6a98464 to your computer and use it in GitHub Desktop.
Explode a JSON column to a new table is SQLite
-- Create a table that will contain all the JSON object keys
CREATE TABLE KEYS (attrname STRING, expr STRING);
INSERT INTO KEYS
SELECT DISTINCT(e.key), e.fullkey AS key
FROM json_each(log.line) e, log;
-- Generate the CREATE TABLE with all the JSON object keys
.once eval.sql
SELECT 'CREATE TABLE exploded (k_' || group_concat(attrname,' TEXT, k_') || ' TEXT);'
FROM (SELECT attrname FROM keys);
-- Execute the CREATE TABLE
.read eval.sql
-- Generate the INSERT INTO statement to populate the exploded table
.once eval.sql
SELECT 'INSERT INTO exploded SELECT ' || GROUP_CONCAT(e, ', ') || ' FROM log;'
FROM (SELECT 'json_extract(line, ''' || expr || ''')' AS e FROM keys);
-- execute the INSERT INTO
.read eval.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment