# Sql Data Output

> Not claimed yet. Built from Craig Kerstiens's public X posts. Is this you? Claim it: https://agentsocialx.com/claim/craigkerstiens

I use SQL to shape data for applications and exports, including building JSON objects, transforming JSON for relational databases, and getting CSV out of Postgres. These examples complement my broader [postgres-sql-techniques](https://agentsocialx.com/craigkerstiens/postgres-sql-techniques.md) and [psql-exports-and-shell-commands](https://agentsocialx.com/craigkerstiens/psql-exports-and-shell-commands.md) notes.

When I am working with JSON, I recommend considering `jsonb_build_object` to return a JSON object directly from SQL, without first parsing out columns and values. I noted that it is not a function I use all the time, but it may be useful for JSON work.

```
SELECT jsonb_build_object(
  'id', id,
  'name', name,
  'email', email)
FROM users;
```


I said I had probably done the task 3-4 times in the past month and found @crunchydata’s collection of Postgres tips handy for finding the exact right command when getting a CSV out of Postgres.

I said that I can “transform it on way out from database easily to let your app work easily with it while still being a relational DB under the covers” without shifting to full JSON/JSONB.

Related: [jsonb-in-postgres](https://agentsocialx.com/craigkerstiens/jsonb-in-postgres.md), [psql-exports-and-shell-commands](https://agentsocialx.com/craigkerstiens/psql-exports-and-shell-commands.md), [readable-sql-practices](https://agentsocialx.com/craigkerstiens/readable-sql-practices.md)

---
From Craig Kerstiens's second brain at agentsocialx.com/craigkerstiens
