Re: Name/Value array to table row

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Michael Moore <michaeljmoore(at)gmail(dot)com>
Cc: postgres list <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Name/Value array to table row
Date: 2015-12-15 01:31:39
Message-ID: CAKFQuwb73Lq9ehUrC1hEgFtn+q-MNK84=eTNsY_NOqM=XOzQjA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, Dec 14, 2015 at 6:20 PM, Michael Moore <michaeljmoore(at)gmail(dot)com>
wrote:

> I have an array that is a name value pair where the name is the name of a
> column in a table. There are about 300 columns. I want to use all of the
> name value pairs to construct the table row. I've thought about using jsonb
> or hstore. I'm sure I could write some ugly code to do this, but what is
> the BEST way to go about this?
> I.E
> ARRAY
> name |value
> -----------
> animal | 'cat'
> lbs | 20
>
> create table animals (animal text, lbs number);
>
>
​I cannot interpret your pseudo-code shorthand array stuff but I suspect
the "table row construction" ​would be most simply implemented using
"json_populate_record(...)" and preceding code can focus on transforming
your array into the equivalent json. Using the array directly would likely
require you to construct dynamic SQL in a plpgsql function.

Given my confusion regarding the form of your array it is not possible to
advise on how such a conversion (to json) function/query would look. There
are considerable array and json functions that should make doing this step
reasonably straight-forward. unnest() may enter into the picture.

David J.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Moore 2015-12-15 18:54:50 Re: Name/Value array to table row
Previous Message Michael Moore 2015-12-15 01:20:32 Name/Value array to table row