diff --git a/README.md b/README.md index 9611d59..2e8a2f4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_531 TILs and counting..._ +_532 TILs and counting..._ --- @@ -297,6 +297,7 @@ _531 TILs and counting..._ - [pg Prefix Is Reserved For System Schemas](postgres/pg-prefix-is-reserved-for-system-schemas.md) - [Prepare, Execute, And Deallocate Statements](postgres/prepare-execute-and-deallocate-statements.md) - [Pretty Print Data Sizes](postgres/pretty-print-data-sizes.md) +- [Pretty Printing JSONB Rows](postgres/pretty-printing-jsonb-rows.md) - [Print The Query Buffer In psql](postgres/print-the-query-buffer-in-psql.md) - [Renaming A Sequence](postgres/renaming-a-sequence.md) - [Renaming A Table](postgres/renaming-a-table.md) diff --git a/postgres/pretty-printing-jsonb-rows.md b/postgres/pretty-printing-jsonb-rows.md new file mode 100644 index 0000000..4c2d1f9 --- /dev/null +++ b/postgres/pretty-printing-jsonb-rows.md @@ -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