I'm appalled by the presence of highly-upvoted misinformation on such a highly-viewed question about a basic topic.

JSON strings cannot be quoted with single quotes. The various versions of the spec (the original by Douglas Crockford, the ECMA version, and the IETF version) all state that strings must be quoted with double quotes. This is not a theoretical issue, nor a matter of opinion as the accepted answer currently suggests; any JSON parser in the real world will error out if you try to have it parse a single-quoted string.

Crockford's and ECMA's version even display the definition of a string using a pretty picture, which should make the point unambiguously clear:

The pretty picture also lists all of the legitimate escape sequences within a JSON string:

  • \"
  • \\
  • \/
  • \b
  • \f
  • \n
  • \r
  • \t
  • \u followed by four-hex-digits

Note that, contrary to the nonsense in some other answers here, \' is never a valid escape sequence in a JSON string. It doesn't need to be, because JSON strings are always double-quoted.

Finally, you shouldn't normally have to think about escaping characters yourself when programatically generating JSON (though of course you will when manually editing, say, a JSON-based config file). Instead, form the data structure you want to encode using whatever native map, array, string, number, boolean, and null types your language has, and then encode it to JSON with a JSON-encoding function. Such a function is probably built into whatever language you're using, like JavaScript's JSON.stringify, PHP's json_encode, or Python's json.dumps. If you're using a language that doesn't have such functionality built in, you can probably find a JSON parsing and encoding library to use. If you simply use language or library functions to convert things to and from JSON, you'll never even need to know JSON's escaping rules. This is what the misguided question asker here ought to have done.

Answer from Mark Amery on Stack Overflow
Top answer
1 of 12
622

I'm appalled by the presence of highly-upvoted misinformation on such a highly-viewed question about a basic topic.

JSON strings cannot be quoted with single quotes. The various versions of the spec (the original by Douglas Crockford, the ECMA version, and the IETF version) all state that strings must be quoted with double quotes. This is not a theoretical issue, nor a matter of opinion as the accepted answer currently suggests; any JSON parser in the real world will error out if you try to have it parse a single-quoted string.

Crockford's and ECMA's version even display the definition of a string using a pretty picture, which should make the point unambiguously clear:

The pretty picture also lists all of the legitimate escape sequences within a JSON string:

  • \"
  • \\
  • \/
  • \b
  • \f
  • \n
  • \r
  • \t
  • \u followed by four-hex-digits

Note that, contrary to the nonsense in some other answers here, \' is never a valid escape sequence in a JSON string. It doesn't need to be, because JSON strings are always double-quoted.

Finally, you shouldn't normally have to think about escaping characters yourself when programatically generating JSON (though of course you will when manually editing, say, a JSON-based config file). Instead, form the data structure you want to encode using whatever native map, array, string, number, boolean, and null types your language has, and then encode it to JSON with a JSON-encoding function. Such a function is probably built into whatever language you're using, like JavaScript's JSON.stringify, PHP's json_encode, or Python's json.dumps. If you're using a language that doesn't have such functionality built in, you can probably find a JSON parsing and encoding library to use. If you simply use language or library functions to convert things to and from JSON, you'll never even need to know JSON's escaping rules. This is what the misguided question asker here ought to have done.

2 of 12
356

A JSON string must be double-quoted, according to the specs, so you don't need to escape '.
If you have to use special character in your JSON string, you can escape it using \ character.

See this list of special character used in JSON :

\b  Backspace (ascii code 08)
\f  Form feed (ascii code 0C)
\n  New line
\r  Carriage return
\t  Tab
\"  Double quote
\\  Backslash character


However, even if it is totally contrary to the spec, the author could use \'.

This is bad because :

  • It IS contrary to the specs
  • It is no-longer JSON valid string

But it works, as you want it or not.

For new readers, always use a double quotes for your json strings.

🌐
TutorialsPoint
tutorialspoint.com › json_simple › json_simple_escape_characters.htm
JSON.simple - Escaping Special Characters
The following characters are reserved characters and can not be used in JSON and must be properly escaped to be used in strings. ... JSONObject.escape() method can be used to escape such reserved keywords in a JSON String. Following is the example − · import org.json.simple.JSONObject; public class JsonDemo { public static void main(String[] args) { JSONObject jsonObject = new JSONObject(); String text = "Text with special character /\"\'\b\f\t\r\n."; System.out.println(text); System.out.println("After escaping."); text = jsonObject.escape(text); System.out.println(text); } }
🌐
Microsoft Learn
learn.microsoft.com › en-us › sql › relational-databases › json › how-for-json-escapes-special-characters-and-control-characters-sql-server
How FOR JSON Escapes Special Characters and Control Characters - SQL Server | Microsoft Learn
This article describes the built-in support for JSON in Microsoft SQL Server. For general information about escaping and encoding in JSON, see Section 2.5 of the JSON RFC. If the source data contains special characters, the FOR JSON clause escapes them in the JSON output with \, as shown in the following table.
🌐
Keploy
keploy.io › home › community › json escape and unescape
JSON Escape and Unescape | Keploy Blog
June 30, 2024 - A beginner's guide to escaping and unescaping JSON strings. Learn when and why to use these methods with simple examples.
🌐
Dynamics Community
community.dynamics.com › forums › thread › details
How to create json with special characters
How to create json with special characters · I don't know if your question is related to Business Central
Find elsewhere
🌐
IETF
ietf.org › rfc › rfc4627.txt
Ietf
JSON-text = object / array These are the six structural characters: begin-array = ws %x5B ws ; [ left square bracket begin-object = ws %x7B ws ; { left curly bracket end-array = ws %x5D ws ; ] right square bracket end-object = ws %x7D ws ; } right curly bracket name-separator = ws %x3A ws ; : colon value-separator = ws %x2C ws ; , comma Insignificant whitespace is allowed before or after any of the six structural characters.
🌐
Tencent Cloud
tencentcloud.com › techpedia › 101732
How to handle special characters in JSON?
Tencent is a leading influencer in industries such as social media, mobile payments, online video, games, music, and more. Leverage Tencent's vast ecosystem of key products across various verticals as well as its extensive expertise and networks to gain a competitive edge and make your own ...
🌐
Medium
medium.com › @learnednwhati › how-to-escape-special-characters-in-building-a-json-string-2b772d6d1194
How to escape special characters in building a JSON string? | by WhatILearned | Medium
December 3, 2019 - How to escape special characters in building a JSON string? The pretty picture also lists all of the legitimate escape sequences within a JSON string: \" \\ \/ \b \f \n \r \t \u followed by …
🌐
Testmu
testmu.ai › home › free tools › json escape
JSON Escape Free Online | Free online tool to convert plain JSON content to escaped HTML.
Click Minify JSON to compress JSON by removing all spaces and line breaks. View the result in the “Output” box below. Use the copy or download buttons to save your output. Special Characters Handling: Escape JSON helps you deal with special characters within your JSON data, ensuring they are properly formatted and won't cause issues during parsing or processing.
🌐
ClarionHub
clarionhub.com › t › hidden-characters-escape-sequences-in-json › 7862
Hidden Characters - Escape Sequences in JSON - ClarionHub
January 25, 2025 - Hello everyone, I’m sending data to a webservice, via POST, PUT request… I’m having problems with hidden characters that originated in the registrations. I need to delete them because the webservice returns “no data s…
🌐
Workato
support.workato.com › en › support › discussions › topics › 1000092420
Enhancement request: Support for Special Characters in JSON keys/names : Workato Success Center
Currently, if the key name in a json key/value pair contains a special character (e.g. periods/dots, colons, hyphens) the data pills are broken as a result and the core use case of using Workato to pass data between steps is broken.
🌐
Technology Org
technology.org › how-and-why › escape-special-characters-json-string
How to Escape Special Characters in JSON String? - Technology Org
July 5, 2023 - Avoiding Syntax Errors: JSON has specific syntax rules, such as using double quotes around keys and string values. If a string value contains unescaped double quotes, it can lead to syntax errors and cause issues during parsing. Preserving Data Accuracy: Some characters, like line breaks or tabs, have special escape sequences (e.g., \n for a line break, \t for a tab) in JSON.
🌐
Google Groups
groups.google.com › g › jstree › c › Ir7g0AY4KQI
Special characters with JSON
I'm dealing with filenames: PHP lists files, converts to JSON which will end up in a jsTree. In german there are a lot of "umlauts" like ä ö ü... Because of them i need to convert between UTF-8 and ISO. - Data received from jsTree (creating, renaming, moving files/folders) needs to convert from UTF-8 using decode_utf8().