Re: Add jsonb_compact(...) for whitespace-free jsonb to text - Mailing list pgsql-hackers

From Ryan Pedela
Subject Re: Add jsonb_compact(...) for whitespace-free jsonb to text
Date
Msg-id CACu89FR43b9C3e-p=zAJ73UD_urhSLOiuYugn-aAK4Wy92=xCA@mail.gmail.com
Whole thread Raw
In response to Add jsonb_compact(...) for whitespace-free jsonb to text  (Sehrope Sarkuni <sehrope@jackdb.com>)
Responses Re: Add jsonb_compact(...) for whitespace-free jsonb to text  (Stephen Frost <sfrost@snowman.net>)
List pgsql-hackers
On Sun, Apr 24, 2016 at 4:02 PM, Sehrope Sarkuni <sehrope@jackdb.com> wrote:
Hi,

The default text representation of jsonb adds whitespace in between key/value pairs (after the colon ":") and after successive properties (after the comma ","):

postgres=# SELECT '{"b":2,"a":1}'::jsonb::text;
       text       
------------------
 {"a": 1, "b": 2}
(1 row)

AFAIK, there's also no guarantee on the specific order of the resulting properties in the text representation either. I would suppose it's fixed for a given jsonb value within a database major version but across major versions it could change (if the underlying representation changes).

I originally ran into this when comparing the hashes of the text representation of jsonb columns. The results didn't match up because the javascript function JSON.stringify(...) does not add any extra whitespace.

It'd be nice to have a stable text representation of a jsonb value with minimal whitespace. The latter would also save a few bytes per record in text output formats, on the wire, and in backups (ex: COPY ... TO STDOUT).

+1

I cannot comment on the patch itself, but I welcome jsonb_compact() or some way to get JSON with no inserted whitespace.

pgsql-hackers by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: [COMMITTERS] pgsql: Add trigonometric functions that work in degrees.
Next
From: Stephen Frost
Date:
Subject: Re: Add jsonb_compact(...) for whitespace-free jsonb to text