JSON Encode Online
ED-58Turn any text into a valid JSON string, or put a whole JSON document inside a string. Decoding a JSON string back to text is included.
Runs in your browser. Nothing is uploaded.
Text, a JSON document, or a JSON string
Result
How to use it
- Paste your text, or a JSON document, into the input box.
- Click Encode text as JSON string to get the text wrapped in quotes with everything escaped.
- Or click Encode JSON as a string to embed a JSON document as a single string value.
- Tick the options if you want non-ASCII written as \uXXXX or slashes escaped as \/.
Encoding text as a string
JSON strings are wrapped in double quotes, and a few characters must be escaped: the quote, the backslash and control characters such as line breaks and tabs. This button applies exactly those rules, so multi-line text, paths and quotes can be pasted into a JSON field without breaking the file. By default non-ASCII characters such as é stay as they are; tick the option to write them as \u escapes.
Embedding JSON inside JSON
Many APIs want a JSON document as a string field, for example a payload field holding stringified JSON. Encode JSON as a string checks that your text is valid JSON, removes the whitespace, then encodes the result as one string. Numbers are kept digit for digit, so a 20 digit ID is not rounded. Invalid JSON is reported before anything is encoded.
Slashes and what this does not do
PHP json_encode escapes the forward slash as \/ unless told not to. The slash option does that for you, and it is optional because \/ and / mean the same to a JSON parser. This tool encodes values you provide. It does not convert PHP objects, classes or other language structures into JSON.
Error messages and what to do
| Message | What it means | Fix |
|---|---|---|
The input has an unpaired surrogate | The text contains half of an emoji, which is not valid Unicode text. | Remove the broken character. |
Unterminated string | When embedding JSON, a string in it is missing its closing quote. | Check the line and column in the message. |
Questions people ask
How do I encode text as JSON?
Paste the text and click Encode text as JSON string. The result is the text in double quotes with quotes, backslashes, line breaks and control characters escaped, ready to be used as a value in a JSON document.
How do I put JSON inside a JSON string?
Paste the JSON and click Encode JSON as a string. It validates the document, strips the whitespace and escapes the whole thing as one string value, which is the format used by fields that carry stringified JSON.
Why are my quotes shown as \"?
Inside a JSON string a double quote must be written as \" so it does not end the string. The backslash is part of the encoding and disappears when the string is parsed or decoded.
Do I have to escape the forward slash?
No. JSON allows / and \/ and they mean the same. Some libraries, including PHP by default, write \/ for safety in HTML. Tick the slash option if you need that output.
How do I decode a JSON string back to text?
Paste the quoted string and click Decode JSON string. Escapes such as \n and \u00e9 are turned back into characters. The surrounding quotes are optional, and invalid escapes are reported.
Is this the same as JSON.stringify?
For a string value, yes: the first button gives what JSON.stringify returns for that text, plus optional slash and ASCII settings. For a whole document, JSON.stringify would parse numbers first and can round big integers, while this keeps them as written.
About this tool
Encode text as JSON string wraps your text in double quotes and escapes quotes, backslashes, line breaks and control characters, so it can be used as a value in a JSON document. Encode JSON as a string first checks that your text is valid JSON and removes the whitespace, then encodes that as one string, which is how JSON is nested inside another JSON field or a request body. Big numbers in that JSON are kept digit for digit. Decode JSON string reverses the first action. The tool encodes values you give it; it does not convert PHP objects or other data structures into JSON.