SQL -> JSON operator
payload->'user' navigates; payload->>'user' navigates and unwraps to text. On PostgreSQL, MySQL and SQLite (3.38+) the arrows are the everyday way to read JSON.
- 类别
- Strings
- Returns
JSON or text- In this category
- 138 of 172
语法
json -> 'key'
json ->> 'key' -- PostgreSQL, SQLite
col -> '$.key' -- MySQL, SQLite
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| SQL Server | Not built in | no arrow operators; JSON_VALUE and JSON_QUERY |
| PostgreSQL | 是 | Supported under this name. |
| MySQL | 是 | column -> path only; the left side must be a column |
| SQLite | 是 | 3.38+; accepts a $ path, a key or an array index |
| Oracle | Not built in | dot-notation or JSON_VALUE |
示例
PostgreSQL
SELECT payload->'user'->>'name' AS name FROM events;
MySQL
SELECT config->>'$.region' AS region FROM settings;
PostgreSQL
SELECT payload->'tags' AS tags_json FROM events;
值得了解
- ->> is not 'dig deeper': it unwraps the JSON quoting into text. Navigate with -> and unwrap only at the leaf you want as text: payload->'user'->>'name', not ->> in the middle.
- PostgreSQL indexes and containment (@>) work on jsonb, not json: store columns as jsonb and cast literals with ::jsonb for the operator path.
- The right-hand side differs: MySQL takes a $ path ('$.a.b'), PostgreSQL takes one key or an integer index per arrow (->'a'->'b', ->0), SQLite takes either. PostgreSQL's ->-1 counts from the end.
- MySQL's arrows are shorthand for JSON_EXTRACT and JSON_UNQUOTE on a column; config->'$.a' works but a literal or expression on the left is a syntax error.
Questions about -> and ->>
When do I use -> versus ->>?
-> when the result feeds more JSON work (further arrows, @>); ->> when it feeds SQL comparisons, ORDER BY or display. So the last arrow in a chain is usually ->>.
Do the arrows work on arrays?
Yes. MySQL: config->>'$.tags[0]'. PostgreSQL: config->'tags'->>0. SQLite: config->>'$.tags[0]'. All zero-based.
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.
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.