SQL JSON_QUOTE and STRING_ESCAPE
JSON_QUOTE('a"b') returns "a\"b": the text wrapped in double quotes with the inner quote escaped, ready to splice into a JSON document.
- Kategorie
- Strings
- Returns
text- In this category
- 169 of 172
Syntax
JSON_QUOTE(s)
json_quote(s)
to_json(s)::text
STRING_ESCAPE(s, 'json')
Each block is a separate form. The support table and the examples show which engine takes which.
Engine-Unterstützung
| Engine | Support | Notizen |
|---|---|---|
| SQL Server | Different name or syntax | STRING_ESCAPE(s, 'json') escapes but adds no surrounding quotes |
| PostgreSQL | Different name or syntax | to_json(s::text)::text |
| MySQL | Ja | JSON_QUOTE(s) |
| SQLite | Ja | json_quote(s) |
| Oracle | Different name or syntax | JSON_SCALAR(s) |
Beispiele
MySQL
SELECT JSON_QUOTE('a"b') AS q; -- "a\"b"
SQLite
SELECT json_quote('a"b') AS q, json_quote(NULL) AS null_input; -- "a\"b", null
PostgreSQL
SELECT to_json('a"b'::text)::text AS q; -- "a\"b"
SQL Server
SELECT '"' + STRING_ESCAPE('a"b', 'json') + '"' AS q; -- "a\"b"
Gut zu wissen
- STRING_ESCAPE escapes quotes, backslashes and control characters but does not add the surrounding double quotes. JSON_QUOTE and json_quote do.
- Building JSON by concatenating strings breaks on the first quote or newline in the data. JSON_OBJECT, json_build_object and FOR JSON do the escaping for you.
- NULL handling differs: SQLite's json_quote(NULL) returns the JSON text null, while MySQL's JSON_QUOTE(NULL) returns SQL NULL.
Questions about JSON_QUOTE
When do I need to quote a string by hand?
Rarely. Prefer the JSON constructors (JSON_OBJECT, json_build_object, FOR JSON PATH), which quote values themselves. Manual quoting fits when you assemble a document from pieces.
Does STRING_ESCAPE support other formats?
Only 'json' as of SQL Server 2025.
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: SQL Server, 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.