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 › home › json_simple › json simple escape characters
JSON.simple - Escaping Special Characters
March 14, 2013 - 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.
🌐
OfflineTools
offlinetools.org › a › json-formatter › handling-special-characters-in-json-strings
Handling Special Characters in JSON Strings | Offline Tools
Validate JSON after generation - Verify that your JSON is valid before using it · Consider Base64 for binary data - Especially for files, images, or binary content · Use forward slashes in paths - Replace backslashes with forward slashes where possible · Be consistent with Unicode - Either use direct Unicode characters or escape sequences, but be consistent · Document your encoding choices - Especially if you use Base64 or other encoding schemes · Handling special ...
🌐
FreeFormatter
freeformatter.com › json-escape.html
Free Online JSON Escape / Unescape Tool - FreeFormatter.com
Escapes or unescapes a JSON string removing traces of offending characters that could prevent parsing.
🌐
SSOJet
ssojet.com › escaping › json-escaping-in-c
JSON Escaping in C# | Escaping Techniques in Programming
The System.Text.Json serializer, built into .NET Core 3.0 and later, takes the guesswork out of JSON escaping. When you serialize a C# object to a JSON string, it automatically handles the necessary escaping for special characters, ensuring ...
Find elsewhere
🌐
JSON Formatter
jsonformatter.org › json-escape
Best JSON Escape Characters, Double Quotes and Backslash tool
JSON Escape Characters tools to escapes double quotes, backslash, single quote and special characters
🌐
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 - 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).
🌐
Stack Overflow
stackoverflow.com › questions › 55187625 › how-to-escape-special-characters-programmatically-while-reading-json-in-c
How to escape special characters programmatically while reading json in C#? - Stack Overflow
It contains invalid json since 'Name': ''!!!' is not valid json. You need to escape the single quote to \'. ... If what you really are talking about when you say "escaping special characters" is how to do a wildcard expression in JPath (i get this suspicion because you list a bunch of !!-ending patterns) then you might try something like this:
🌐
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
🌐
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.
🌐
CodeGenes
codegenes.net › blog › how-to-escape-special-characters-in-building-a-json-string
How to Escape Special Characters in JSON Strings: Handling User Input to Prevent Broken Code — codegenes.net
These libraries are maintained, tested, and handle all edge cases (e.g., rare control characters like \f). While escaping fixes syntax issues, validate user input first to catch invalid or malicious content. For example: Reject excessively long strings to prevent DoS attacks. Sanitize inputs to remove dangerous patterns (e.g., SQL injection fragments, though JSON itself isn’t executable). Test your JSON handling with user input containing all special 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 ...