mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Pretty Printing JSONB Rows as a postgres til
This commit is contained in:
37
postgres/pretty-printing-jsonb-rows.md
Normal file
37
postgres/pretty-printing-jsonb-rows.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# Pretty Printing JSONB Rows
|
||||
|
||||
Who needs a document store when you can just use PostgreSQL's `JSONB` data
|
||||
type? Viewing rows of `JSONB` output can be challenging though because it
|
||||
defaults to printing them as a single line of text.
|
||||
|
||||
```sql
|
||||
> select '{"what": "is this", "nested": {"items 1": "are the best", "items 2": [1, 2, 3]}}'::jsonb;
|
||||
jsonb
|
||||
----------------------------------------------------------------------------------
|
||||
{"what": "is this", "nested": {"items 1": "are the best", "items 2": [1, 2, 3]}}
|
||||
(1 row)
|
||||
```
|
||||
|
||||
Fortunately, Postgres comes with a function for prettying up the format of
|
||||
the output of these rows --
|
||||
[`jsonb_pretty`](https://www.postgresql.org/docs/current/static/functions-json.html)
|
||||
|
||||
```sql
|
||||
> select jsonb_pretty('{"what": "is this", "nested": {"items 1": "are the best", "items 2": [1, 2, 3]}}'::jsonb);
|
||||
jsonb_pretty
|
||||
------------------------------------
|
||||
{ +
|
||||
"what": "is this", +
|
||||
"nested": { +
|
||||
"items 1": "are the best",+
|
||||
"items 2": [ +
|
||||
1, +
|
||||
2, +
|
||||
3 +
|
||||
] +
|
||||
} +
|
||||
}
|
||||
(1 row)
|
||||
```
|
||||
|
||||
h/t Jack Christensen
|
||||
Reference in New Issue
Block a user