Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chetanppatil/191cbc1c1b447f894a5cc65e61d0cb5f to your computer and use it in GitHub Desktop.
Save chetanppatil/191cbc1c1b447f894a5cc65e61d0cb5f to your computer and use it in GitHub Desktop.
Select data from json and insert it into single row and multiple column
CREATE TABLE my_table(col text, col2 text)
/* SELECT DATA FROM JSON */
SELECT * FROM json_each('{"a":"foo", "b":"bar"}') AS e
/* INSERT INTO TABLE */
INSERT INTO my_table(col, col2)
SELECT * FROM json_to_record('{"a":"foo", "b":"bar"}') AS x(col text, col2 text) -- as can have different column name
SELECT * FROM my_table
/* UPDATE RECORD IN my_table */
UPDATE my_table
SET col = x.a, col2 = x.b
FROM json_to_record('{"a":"11111", "b":"bar222"}') AS x(a text, b text)
WHERE col='foo'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment