SELECT '[]'::JSON; json ------ [] (1 row) SELECT '['::JSON; ERROR: could not parse JSON value LINE 1: SELECT '['::JSON; ^ SELECT '[1,2,3]'::JSON; json --------- [1,2,3] (1 row) SELECT '[1,2,3]'::JSON::TEXT; text --------- [1,2,3] (1 row) SELECT '[1,2,3 ]'::JSON; json ----------- [1,2,3 ] (1 row) SELECT '[1,2,3 ,4]'::JSON; json ------------- [1,2,3 ,4] (1 row) SELECT '[1,2,3 ,4.0]'::JSON; json --------------- [1,2,3 ,4.0] (1 row) SELECT '[1,2,3 ,4]'::JSON; json ------------- [1,2,3 ,4] (1 row) SELECT 'true'::JSON; json ------ true (1 row) SELECT 'true'::TEXT::JSON; json ------ true (1 row) SELECT 'false'::JSON; json ------- false (1 row) SELECT 'null'::JSON; json ------ null (1 row) SELECT '1.1'::JSON; json ------ 1.1 (1 row) SELECT '"string"'::JSON; json ---------- "string" (1 row) SELECT '{"key1":"value1", "key2":"value2"}'::JSON; json ------------------------------------ {"key1":"value1", "key2":"value2"} (1 row) SELECT '{"key1":"value1", "key2":"value2"}'::JSON; json ------------------------------------ {"key1":"value1", "key2":"value2"} (1 row) SELECT 15::JSON; json ------ 15 (1 row) SELECT json_get_type('[]'); json_get_type --------------- array (1 row) SELECT json_get_type('{}'); json_get_type --------------- object (1 row) SELECT json_get_type('true'); json_get_type --------------- bool (1 row) SELECT json_get_type('false'); json_get_type --------------- bool (1 row) SELECT json_get_type('null'); json_get_type --------------- null (1 row) CREATE TABLE testjson (j JSON); INSERT INTO testjson VALUES ('[1,2,3,4]'); INSERT INTO testjson VALUES ('{"key":"value"}'); INSERT INTO testjson VALUES ('{"key":"value"'); ERROR: could not parse JSON value LINE 1: INSERT INTO testjson VALUES ('{"key":"value"'); ^ INSERT INTO testjson VALUES (''); ERROR: could not parse JSON value LINE 1: INSERT INTO testjson VALUES (''); ^ INSERT INTO testjson VALUES ('""'); INSERT INTO testjson VALUES ('true'); INSERT INTO testjson VALUES ('false'); INSERT INTO testjson VALUES ('null'); INSERT INTO testjson VALUES ('[]'); INSERT INTO testjson VALUES ('{}'); SELECT * FROM testjson; j ----------------- [1,2,3,4] {"key":"value"} "" true false null [] {} (8 rows) SELECT json_get_type(j) FROM testjson; json_get_type --------------- array object string bool bool null array object (8 rows) -- to_json: null SELECT to_json(NULL); ERROR: could not determine polymorphic type because input has type "unknown" SELECT to_json(NULL::INT); to_json --------- null (1 row) SELECT to_json(NULL::INT[]); to_json --------- null (1 row) SELECT to_json(NULL::VOID); ERROR: to_json cannot convert void to JSON SELECT to_json(NULL::MONEY); ERROR: to_json cannot convert money to JSON SELECT to_json('null'::text); -- should yield '"null"', not 'null' to_json --------- "null" (1 row) SELECT json_get_type(to_json(NULL)); ERROR: could not determine polymorphic type because input has type "unknown" SELECT json_get_type(to_json(NULL::INT)); json_get_type --------------- null (1 row) SELECT json_get_type(to_json(NULL::VOID)); ERROR: to_json cannot convert void to JSON SELECT json_get_type(to_json(NULL::MONEY)); ERROR: to_json cannot convert money to JSON -- to_json: string SELECT to_json(''); ERROR: could not determine polymorphic type because input has type "unknown" SELECT json_get_type(to_json('')); ERROR: could not determine polymorphic type because input has type "unknown" SELECT to_json('string'); ERROR: could not determine polymorphic type because input has type "unknown" SELECT json_get_type(to_json('string')); ERROR: could not determine polymorphic type because input has type "unknown" SELECT to_json('string'::VARCHAR); to_json ---------- "string" (1 row) SELECT json_get_type(to_json('string'::VARCHAR)); json_get_type --------------- string (1 row) SELECT to_json('string'::VARCHAR(3)); to_json --------- "str" (1 row) SELECT json_get_type(to_json('string'::VARCHAR(3))); json_get_type --------------- string (1 row) SELECT to_json('{1,2,3}'::TEXT); to_json ----------- "{1,2,3}" (1 row) SELECT json_get_type(to_json('{1,2,3}'::TEXT)); json_get_type --------------- string (1 row) SELECT to_json('"doubly-encoded"'::JSON); to_json ---------------------- "\"doubly-encoded\"" (1 row) SELECT json_get_type(to_json('"doubly-encoded"'::JSON)); json_get_type --------------- string (1 row) SELECT to_json('"nested quotes"'::TEXT); to_json --------------------- "\"nested quotes\"" (1 row) SELECT json_get_type(to_json('"nested quotes"'::TEXT)); json_get_type --------------- string (1 row) SELECT to_json('"nested quotes"'::TEXT)::TEXT::JSON; to_json --------------------- "\"nested quotes\"" (1 row) SELECT json_get_type(to_json('"nested quotes"'::TEXT)::TEXT::JSON); json_get_type --------------- string (1 row) SELECT to_json('h'::CHAR); to_json --------- "h" (1 row) SELECT json_get_type(to_json('h'::CHAR)); json_get_type --------------- string (1 row) SELECT to_json('hello world'::CHAR); to_json --------- "h" (1 row) SELECT json_get_type(to_json('hello world'::CHAR)); json_get_type --------------- string (1 row) SELECT to_json('hello world!'::CHAR(11)); to_json --------------- "hello world" (1 row) SELECT json_get_type(to_json('hello world!'::CHAR(11))); json_get_type --------------- string (1 row) -- to_json: number SELECT to_json(12345); to_json --------- 12345 (1 row) SELECT to_json(12345.678); to_json ----------- 12345.678 (1 row) SELECT json_get_type(to_json(12345)); json_get_type --------------- number (1 row) SELECT json_get_type(to_json(12345.678)); json_get_type --------------- number (1 row) SELECT to_json(+1.23e100::FLOAT); to_json ----------- 1.23e+100 (1 row) SELECT to_json('+1.23e100'::FLOAT); to_json ----------- 1.23e+100 (1 row) SELECT to_json(123456789012345678901234567890123456789012345678901234567890.123456789012345678901234567890123456789012345678901234567890); to_json --------------------------------------------------------------------------------------------------------------------------- 123456789012345678901234567890123456789012345678901234567890.123456789012345678901234567890123456789012345678901234567890 (1 row) SELECT json_get_type(to_json(123456789012345678901234567890123456789012345678901234567890.123456789012345678901234567890123456789012345678901234567890)); json_get_type --------------- number (1 row) SELECT to_json('100'::MONEY); ERROR: to_json cannot convert money to JSON -- to_json: bool SELECT to_json(TRUE); to_json --------- true (1 row) SELECT to_json(FALSE); to_json --------- false (1 row) SELECT to_json(1=1); to_json --------- true (1 row) SELECT to_json(1=2); to_json --------- false (1 row) SELECT json_get_type(to_json(TRUE)); json_get_type --------------- bool (1 row) SELECT json_get_type(to_json(FALSE)); json_get_type --------------- bool (1 row) SELECT json_get_type(to_json(1=1)); json_get_type --------------- bool (1 row) SELECT json_get_type(to_json(1=2)); json_get_type --------------- bool (1 row) SELECT to_json(TRUE::TEXT)::TEXT = '"' || TRUE || '"'; ?column? ---------- t (1 row) SELECT to_json(FALSE::TEXT)::TEXT = '"' || FALSE || '"'; ?column? ---------- t (1 row) -- to_json: array SELECT to_json(ARRAY[1,2,3]); to_json --------- [1,2,3] (1 row) -- more tests are in array_to_json.sql -- to_json: invalid types SELECT to_json(row(1,2)); ERROR: to_json cannot convert record to JSON SELECT to_json('127.0.0.1'::INET); ERROR: to_json cannot convert inet to JSON -- from_json: null SELECT from_json(null); from_json ----------- (1 row) SELECT from_json(NULL::TEXT); ERROR: function from_json(text) does not exist LINE 1: SELECT from_json(NULL::TEXT); ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. SELECT from_json(NULL::JSON); from_json ----------- (1 row) -- from_json: string SELECT from_json('"valid string"'); from_json -------------- valid string (1 row) -- Use unaligned output so results are consistent between PostgreSQL 8 and 9. \a SELECT from_json($$ "hello\nworld" $$); from_json hello world (1 row) \a SELECT from_json($$ "hello\u0000world" $$); ERROR: could not convert JSON-encoded string to TEXT: contains \u0000 DETAIL: the string was: "hello\u0000world" -- from_json: number SELECT from_json('123'); from_json ----------- 123 (1 row) SELECT from_json('123')::INT; from_json ----------- 123 (1 row) SELECT from_json('123.456')::INT; ERROR: invalid input syntax for integer: "123.456" SELECT from_json('123.456')::FLOAT; from_json ----------- 123.456 (1 row) SELECT from_json('123e-38'); from_json ----------- 123e-38 (1 row) SELECT from_json('123e-38')::FLOAT; from_json ----------- 1.23e-36 (1 row) SELECT from_json('1.23e-38')::FLOAT; from_json ----------- 1.23e-38 (1 row) SELECT from_json('1.23e-38'); from_json ----------- 1.23e-38 (1 row) SELECT from_json('1.23e-38')::NUMERIC; from_json -------------------------------------------- 0.0000000000000000000000000000000000000123 (1 row) -- from_json: bool SELECT from_json('true')::JSON; from_json ----------- true (1 row) SELECT from_json('true'); from_json ----------- true (1 row) SELECT from_json('true')::BOOLEAN; from_json ----------- t (1 row) SELECT from_json('true')::BOOLEAN::JSON; ERROR: could not parse JSON value SELECT from_json('true')::BOOLEAN::TEXT::JSON; from_json ----------- true (1 row) SELECT from_json('false')::BOOLEAN::TEXT::JSON; from_json ----------- false (1 row) SELECT from_json('false'); from_json ----------- false (1 row) SELECT from_json('f'); ERROR: could not parse JSON value LINE 1: SELECT from_json('f'); ^ SELECT from_json('t'); ERROR: could not parse JSON value LINE 1: SELECT from_json('t'); ^ SELECT from_json('f'::BOOLEAN::TEXT); ERROR: function from_json(text) does not exist LINE 1: SELECT from_json('f'::BOOLEAN::TEXT); ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. SELECT from_json('f'::BOOLEAN::TEXT::JSON); from_json ----------- false (1 row) SELECT from_json('t'::BOOLEAN::TEXT::JSON); from_json ----------- true (1 row) -- from_json: object SELECT from_json('{"key": "value"}'); ERROR: from_json cannot be applied to JSON objects -- from_json: array SELECT from_json('[1,2,3]'); ERROR: from_json cannot be applied to JSON arrays -- from_json: invalid SELECT from_json('invalid'); ERROR: could not parse JSON value LINE 1: SELECT from_json('invalid'); ^ CREATE TABLE sample_query_text (json JSON); INSERT INTO sample_query_text VALUES ($$ "SELECT pg_catalog.quote_ident(c.relname) FROM pg_catalog.pg_class c WHERE c.relkind IN ('i') AND substring(pg_catalog.quote_ident(c.relname),1,0)='' AND pg_catalog.pg_table_is_visible(c.oid) AND c.relnamespace <> (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')\nUNION\nSELECT pg_catalog.quote_ident(n.nspname) || '.' FROM pg_catalog.pg_namespace n WHERE substring(pg_catalog.quote_ident(n.nspname) || '.',1,0)='' AND (SELECT pg_catalog.count(*) FROM pg_catalog.pg_namespace WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,0) = substring('',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1\nUNION\nSELECT pg_catalog.quote_ident(n.nspname) || '.' || pg_catalog.quote_ident(c.relname) FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n WHERE c.relnamespace = n.oid AND c.relkind IN ('i') AND substring(pg_catalog.quote_ident(n.nspname) || '.' || pg_catalog.quote_ident(c.relname),1,0)='' AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,0) = substring('',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1) AND (SELECT pg_catalog.count(*) FROM pg_catalog.pg_namespace WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,0) = substring('',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1\n UNION SELECT 'ON' UNION SELECT 'CONCURRENTLY'\nLIMIT 1000" $$); SELECT md5(from_json(json)) FROM sample_query_text; md5 ---------------------------------- 1c0d28af72da21149acbdf305c197141 (1 row)