SQL JSON_PRETTY
Compact JSON is hard to read in a result grid. Four of the five engines can indent it for you.
- 類別
- Strings
- Returns
text- In this category
- 168 of 172
語法
JSON_PRETTY(doc)
jsonb_pretty(doc)
json_pretty(doc [, indent])
JSON_SERIALIZE(doc PRETTY)
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | Not built in | no pretty-printer; FOR JSON and JSON_QUERY return compact text |
| PostgreSQL | Different name or syntax | jsonb_pretty(jsonb) |
| MySQL | 是 | JSON_PRETTY(doc) |
| SQLite | 是 | json_pretty(doc [, indent]) from 3.46 |
| Oracle | Different name or syntax | JSON_SERIALIZE(doc PRETTY) |
範例
MySQL
SELECT JSON_PRETTY('{"a":1,"b":[1,2]}') AS pretty; -- two-space indent
PostgreSQL
SELECT jsonb_pretty('{"a":1,"b":[1,2]}'); -- four-space indent
SQLite
SELECT json_pretty('{"a":1,"b":[1,2]}', ' ') AS pretty;
Oracle
SELECT JSON_SERIALIZE('{"a":1,"b":[1,2]}' PRETTY) AS pretty FROM dual;
小知識
- Indentation differs: MySQL and Oracle indent by two spaces, PostgreSQL and SQLite by four. Oracle also puts an array's opening bracket on its own line.
- jsonb_pretty takes jsonb, so the output follows jsonb rules: keys are reordered and only the last of duplicate keys survives.
- SQLite's json_pretty takes an optional indent string: json_pretty(doc, ' ') indents by two spaces.
Questions about JSON_PRETTY
How do I compact JSON instead?
SQLite: json(doc) strips the whitespace. Oracle: JSON_SERIALIZE(doc) without PRETTY. PostgreSQL's jsonb and MySQL's JSON type store a normalized form and print it on one line.
Does pretty-printing change the stored value?
No. These functions return new text; the column keeps whatever was stored.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.