About the JSON Encode
Encode text (or a JSON document) as a JSON string literal, escaping quotes, backslashes, newlines and control characters.
Key features
- Wraps any text in a valid JSON string literal
- Escapes quotes, backslashes, line breaks and control characters
- Optionally minifies JSON input before encoding
- Unicode-safe: emoji and accents are preserved
- Output can be embedded in any JSON document
- Instant and private
What is JSON encoding?
JSON encoding turns arbitrary text into a JSON string literal: the text wrapped in double quotes with every special character properly escaped. You need this whenever text has to live inside JSON: a message body, an HTML snippet in a CMS payload, a code sample in an API request, or a JSON document stored as a string field.
Example
Text:
He said "hi"
C:\temp\file.txt
JSON-encoded:
"He said \"hi\"\nC:\\temp\\file.txt"
What gets escaped
| Original | Encoded |
|---|---|
" | \" |
\ | \\ |
| new line | \n |
| carriage return | \r |
| tab | \t |
| control chars (U+0000–U+001F) | \u0000 … \u001f |
é, 😀, / | unchanged |
Common uses
- API requests. Put HTML, Markdown or multi-line text into a JSON body.
- Templating. Inject server values into a
<script>tag safely (combine with HTML escaping of<). - Config files. Store multi-line values such as certificates or SQL in
appsettings.jsonorpackage.jsonscripts. - Testing. Build fixtures where a field contains embedded JSON.
Encoding in code
const literal = JSON.stringify(text);
literal = json.dumps(text, ensure_ascii=False)
var literal = JsonSerializer.Serialize(text);
Related tools
Reverse it with JSON Decode. To get the escaped content without surrounding quotes, use JSON Escape / Unescape. To produce a string literal for a specific programming language, use JSON to String.
How to use the JSON Encode
- Paste your data into the input box, upload a file, or click Sample to try it out.
- Adjust the options above the editor if you need to (indentation, delimiter, and so on).
- The result appears instantly in the output panel. Any errors are shown with their line and column.
- Click Copy to copy the result, or Download to save it as a file.