Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TQve5-0001LE-UI for pgsql-bugs@postgresql.org; Wed, 24 Oct 2012 07:44:58 +0000 Received: from mail-pb0-f46.google.com ([209.85.160.46]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TQvdz-0005Xp-8h for pgsql-bugs@postgresql.org; Wed, 24 Oct 2012 07:44:57 +0000 Received: by mail-pb0-f46.google.com with SMTP id rr4so1039884pbb.19 for ; Wed, 24 Oct 2012 00:44:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=QyI4md69nzJhyE8GSQFPxAxaNvUpxiNh5NszBrH9FJM=; b=fU/Z/gpeQOhML8KoNfszTW7PiKFrFsnaOA3kwf0eqg4BNnb9j96hYAhv+woqU/pdwT 9Lx6QgxB30zus1VvG0xXTf3H7pPgyv7M8OMbkqSsW7/28AcU9hLqB1Zd2p0QQM/OA+NN nRJ46FAHGCmL2PwskMEkH1s0MUOvAhljwIZvYMrnrFLEUQ9yozAye0b+OraTCBA+t/nO IzzWtGYWivDH+qE1E0efFMZXIcE8uwNktI9oe6tOS8Ku4X0afnla/dcNgeYfDMqE28gJ soSs08Uwf6npME1owUB03KQ1hllfmzwCSyd0PBoubyaqTyeJ7EQWiokDE5vkMEOed4qE gK+w== Received: by 10.66.86.102 with SMTP id o6mr41931281paz.11.1351064689498; Wed, 24 Oct 2012 00:44:49 -0700 (PDT) Received: from [192.168.0.6] (c-98-248-160-201.hsd1.ca.comcast.net. [98.248.160.201]) by mx.google.com with ESMTPS id k9sm9095381paz.22.2012.10.24.00.44.47 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 24 Oct 2012 00:44:48 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) Subject: Re: BUG #7620: array_to_json doesn't support heterogeneous arrays From: Greg Hazel In-Reply-To: <50879711.6020209@ringerc.id.au> Date: Wed, 24 Oct 2012 00:44:47 -0700 Cc: pgsql-bugs@postgresql.org Content-Transfer-Encoding: 7bit Message-Id: References: <50879711.6020209@ringerc.id.au> To: Craig Ringer X-Mailer: Apple Mail (2.1499) X-Pg-Spam-Score: -2.7 (--) X-Archive-Number: 201210/214 X-Sequence-Number: 34514 On Oct 24, 2012, at 12:21 AM, Craig Ringer wrote: > On 10/24/2012 07:32 AM, ghazel@gmail.com wrote: >> The following bug has been logged on the website: >> >> Bug reference: 7620 >> Logged by: Greg Hazel >> Email address: ghazel@gmail.com >> PostgreSQL version: 9.2.1 >> Operating system: Amazon Linux >> Description: >> >> array_to_json(ARRAY['foo', 100, true]) complains because arrays can't have >> mixed types, but json arrays can. > > The issue here isn't array_to_json, it's PostgreSQL arrays. > > What you appear to want is a way to call row_to_json so that it produces > a json array instead of a json object as it currently does. That way you > could pass it a ROW() construct, composite type, or record, and have it > output a heterogeneous JSON array. > > This isn't a bug, but it's a perfectly reasonable feature request if > re-interpreted a little. It will never work with PostgreSQL arrays, > though, because the arrays themselves cannot contain mixed types: > > regress=# SELECT ARRAY[1,'test']; > ERROR: invalid input syntax for integer: "test" > LINE 1: SELECT ARRAY[1,'test']; > ^ > Instead you want a way to take this: > > regress=# SELECT ROW(1,'test'); > row > ---------- > (1,test) > (1 row) > > and output the json: > > [1,"test"] > > instead of a json object: > > regress=# SELECT row_to_json(ROW(1,'test')); > row_to_json > ---------------------- > {"f1":1,"f2":"test"} > (1 row) > > > Would a version of `row_to_json` that output a json array satisfy your > needs? Sure, that would be fine. -Greg