I am using Postgres 9.5, and I am trying to figure out how to update a postgres table using an array of JSON. I want each object in the array to coorespond to a new row, with each key cooresponding to a column, and each value is the data to be inserted into the column. I am trying to do so with a function. Here the the data format:
[
{ col1: a, col2: 5, col3: 1, col4: one},
{ col1: b, col2: 6, col3: 2, col4: two},
{ col1: c, col2: 7, col3: 3, col4: three},
{ col1: d, col2: 8, col3: 4, col4: four},
]
Here is my expected output:
col1 (varchar)| col2 (integer) | col3 (integer) | col4 (varchar)
-----------------+----------------+--------------------+------------------
a | 5 | 1 | one
b | 6 | 2 | two
c | 7 | 3 | three
d | 8 | 4 | four
Is there a built in postgres JSON function or operator that can do this for me? Or do I have to loop through the array and pull out each value, and pass it as an input? I know function below is wrong, but my goal is for the function to act similar to the following:
CREATE OR REPLACE FUNCTION UPDATE_TABLE_FUNC (
arrayOfValues TEXT[]
)
RETURN VOID AS $$
BEGIN
UPDATE table SET (col1, col2, col3, col4) = ($1)
END;
$$ LANGUAGE plpgsql;