JSON Encode

Encode a value as JSON text with the same switches PHP and Python expose, including ASCII only output and escaped slashes.

Data
Encoded JSON
Runs locally in your browser

About the JSON Encode

Encoding is the step that turns a value in memory into transportable text. This page does the same job for data you already have on the clipboard: paste a value, choose how strict the output should be, and take the encoded string away. Anything that parses as JSON is re-encoded as a structure; anything else is encoded as a single JSON string, which is the behaviour you want when you need to embed a message or a template in a field.

"Escape non-ASCII as \uXXXX" mirrors the default of PHP json_encode and Python json.dumps: every character above the ASCII range becomes a six character escape, so the payload survives a transport that mangles UTF-8. "Escape forward slashes" writes \/ instead of /, which is what you need when the JSON will be pasted inside an HTML script block, because a literal </script> inside a string would otherwise close the tag early.

Pretty printing is off by default because encoded output is normally meant for a wire or a database column rather than for reading. Switch it on with an indent width when you are writing a fixture file that a person will review.

The status line counts the characters in the result, which is handy when a column has a length limit. To go back the other way, use JSON Decode.

How to use

  1. Paste the data you want encoded. Structured input is re-encoded; anything else becomes one JSON string.
  2. Set Treat input as to Plain text if you want a paragraph or a template encoded literally, even when it happens to look like JSON.
  3. Tick the escaping switches your target needs, then read the character count in the status line.
  4. Copy the encoded text or download it as encoded.json.

Common questions

When should I escape forward slashes?
When the JSON is embedded in an HTML page inside a script tag. Escaping the slash prevents the closing tag sequence from appearing inside a string and ending the script early.
Does escaping non-ASCII characters change the data?
No. A \uXXXX escape and the character itself parse to exactly the same string; only the wire format differs.
How do I encode a plain sentence as a JSON value?
Set "Treat input as" to Plain text. The whole input, newlines included, comes back as one quoted and escaped string.
Why is the output on a single line by default?
Encoded JSON is normally stored or transmitted rather than read, and one line is the compact form. Tick Pretty print when a human needs to review it.