Re: Unexpected syntax error when using JSON -> in 9.3.5 - Mailing list pgsql-general

From Tom Lane
Subject Re: Unexpected syntax error when using JSON -> in 9.3.5
Date
Msg-id 14440.1411490484@sss.pgh.pa.us
Whole thread Raw
In response to Unexpected syntax error when using JSON -> in 9.3.5  (Seamus Abshere <seamus@abshere.net>)
List pgsql-general
Seamus Abshere <seamus@abshere.net> writes:
>> foobar=# select coalesce('{}'::json->'a', 1);
>> ERROR:  COALESCE types json and integer cannot be matched

Right ...

> but check it out when I use a string instead of an integer:

>> foobar=# select coalesce('{}'::json->'a', 'b');
>> ERROR:  invalid input syntax for type json

This is expected.  The parser correctly determines that the json->text
operator returns json, and it knows that the two arguments of coalesce
must resolve as the same type, so it tries to coerce the untyped literal
to json --- which means passing that string to the json input function.
A quoted literal is not automatically a string in Postgres, it can get
resolved as some other type if required by context.  In the first example
the integer literal has a well-defined type, but bare quoted literals are
not considered to have a known type.

            regards, tom lane


pgsql-general by date:

Previous
From: David G Johnston
Date:
Subject: Re: Unexpected syntax error when using JSON -> in 9.3.5
Next
From: Tom Lane
Date:
Subject: Re: Will there be a JSON operator like ->> but returning numeric?