1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Add 'ignore_nulls' option to row_to_json

Provide an option to skip NULL values in a row when generating a JSON
object from that row with row_to_json.  This can reduce the size of the
JSON object in cases where columns are NULL without really reducing the
information in the JSON object.

This also makes row_to_json into a single function with default values,
rather than having multiple functions.  In passing, change array_to_json
to also be a single function with default values (we don't add an
'ignore_nulls' option yet- it's not clear that there is a sensible
use-case there, and it hasn't been asked for in any case).

Pavel Stehule
This commit is contained in:
Stephen Frost
2014-09-11 21:23:51 -04:00
parent c3c75fcd7a
commit 95d737ff45
8 changed files with 118 additions and 51 deletions

View File

@ -397,12 +397,70 @@ FROM rows q;
"y":"txt3"}
(3 rows)
SELECT row_to_json(q,pretty := true)
FROM rows q;
row_to_json
--------------
{"x":1, +
"y":"txt1"}
{"x":2, +
"y":"txt2"}
{"x":3, +
"y":"txt3"}
(3 rows)
SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
row_to_json
-----------------------
{"f1":[5,6,7,8,9,10]}
(1 row)
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
(10,NULL, NULL),
(NULL, NULL, NULL)) g(a,b,c))
SELECT row_to_json(x, false, false) FROM x;
row_to_json
------------------------------
{"a":10,"b":20,"c":30}
{"a":10,"b":null,"c":null}
{"a":null,"b":null,"c":null}
(3 rows)
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
(10,NULL, NULL),
(NULL, NULL, NULL)) g(a,b,c))
SELECT row_to_json(x, false, true) FROM x;
row_to_json
------------------------
{"a":10,"b":20,"c":30}
{"a":10}
{}
(3 rows)
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
(10,NULL, NULL),
(NULL, NULL, NULL)) g(a,b,c))
SELECT row_to_json(x, ignore_nulls := true) FROM x;
row_to_json
------------------------
{"a":10,"b":20,"c":30}
{"a":10}
{}
(3 rows)
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
(10,NULL, NULL),
(NULL, NULL, NULL)) g(a,b,c))
SELECT row_to_json(x, ignore_nulls := true, pretty := true) FROM x;
row_to_json
-------------
{"a":10, +
"b":20, +
"c":30}
{"a":10}
{}
(3 rows)
-- to_json, timestamps
select to_json(timestamp '2014-05-28 12:22:35.614298');
to_json

View File

@ -98,8 +98,32 @@ FROM generate_series(1,3) AS x;
SELECT row_to_json(q,true)
FROM rows q;
SELECT row_to_json(q,pretty := true)
FROM rows q;
SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
(10,NULL, NULL),
(NULL, NULL, NULL)) g(a,b,c))
SELECT row_to_json(x, false, false) FROM x;
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
(10,NULL, NULL),
(NULL, NULL, NULL)) g(a,b,c))
SELECT row_to_json(x, false, true) FROM x;
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
(10,NULL, NULL),
(NULL, NULL, NULL)) g(a,b,c))
SELECT row_to_json(x, ignore_nulls := true) FROM x;
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
(10,NULL, NULL),
(NULL, NULL, NULL)) g(a,b,c))
SELECT row_to_json(x, ignore_nulls := true, pretty := true) FROM x;
-- to_json, timestamps
select to_json(timestamp '2014-05-28 12:22:35.614298');