Created
February 28, 2021 20:47
-
-
Save drewcsillag/1bbf146f30a11adfee02c911b6a98464 to your computer and use it in GitHub Desktop.
Explode a JSON column to a new table is SQLite
This file contains 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
-- 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