Re: row_to_json question - Mailing list pgsql-general

From Joe Van Dyk
Subject Re: row_to_json question
Date
Msg-id CACfv+pLGDodRthgTdWOCOQgin2O5KXkn4Pv_EREN2c5MPdKQRw@mail.gmail.com
Whole thread Raw
In response to row_to_json question  (Joe Van Dyk <joe@tanga.com>)
Responses Re: row_to_json question  (Raghavendra <raghavendra.rao@enterprisedb.com>)
Re: row_to_json question  (Merlin Moncure <mmoncure@gmail.com>)
List pgsql-general
On Sat, Jun 23, 2012 at 3:03 PM, Joe Van Dyk <joe@tanga.com> wrote:
> How can I use row_to_json for a subset of columns in a row? (without
> creating a new view or using a CTE?)
>
> What I want returned:
> {"email_address":"joe@tanga.com","username":"joevandyk"}
> Note that there is no "id" column in the result.
>
>
> create table users (id serial primary key, email_address varchar,
> username varchar);
> insert into users (email_address, username) values ('joe@tanga.com',
> 'joevandyk');

This is the best I can come up with:

select row_to_json(f) from (select email_address, username from users) f;
    {"email_address":"joe@tanga.com","username":"joevandyk"}

Is there a cleaner way to do this?

pgsql-general by date:

Previous
From: Joe Van Dyk
Date:
Subject: row_to_json question
Next
From: Jasen Betts
Date:
Subject: Re: efficiency of wildcards at both ends