What is the equivalent of this MySQL function
SELECT JSON_PRETTY('{"a": 1, "b": 2, "c": 3}') AS Result
FROM table;
Formatted JSON:
+------------------------------+
| Result |
+------------------------------+
| { |
| "a": 1, |
| "b": 2, |
| "c": 3 |
| } |
+------------------------------+
I've tried jsonb_pretty()
as mentioned in the document but nothing is available
You want
jsonb_pretty()
, that is available in Postgres since version 9.5:Demo on DB Fiddle:
The
jsonb
type forces internal key order, so casting to it might reorder yourjson
. It also discards whitespace between tokens and keeps only the last value for duplicate keys. Casting fromjson
tojsonb
and back will not restore the original order, whitespace or the duplicate keys.jsonb_pretty()
works onjsonb
datatype only. If your input isjson
, you need to cast it first. As a formatting function, it returns typetext
, similar to how MySQL'sjson_pretty()
returnslongtext
. Cast it back tojson
/jsonb
or revert to the unprocessed value to continue using json functions and operators.