When I try to access a JSON element using ->>'elementName'
I get a text
type.
SELECT pg_typeof(x1->>'a'), jsonb_typeof(x2)
FROM ( VALUES
('{"a":5}'::jsonb, '5'::jsonb)
) AS t(x1,x2);
pg_typeof | jsonb_typeof
-----------+--------------
text | number
(1 row)
However, jsonb
says it maps numbers to numeric types...
When converting textual JSON input into jsonb, the primitive types described by RFC 7159 are effectively mapped onto native PostgreSQL types, as shown in Table 8-23.
Here is the table reproduced from the docs,
Table 8-23. JSON primitive types and corresponding PostgreSQL types
JSON primitive type PostgreSQL type Notes
string text \u0000 is disallowed, as are non-ASCII Unicode escapes if database encoding is not UTF8
number numeric NaN and infinity values are disallowed
boolean boolean Only lowercase true and false spellings are accepted
null (none) SQL NULL is a different concept