Why stringify JSON
Sometimes a whole JSON document has to travel as a single string value. Common cases:
- An environment variable or Kubernetes secret that holds a JSON config, such as
GOOGLE_CREDENTIALSor a feature-flag payload. - A field in another JSON document, such as an SQS or SNS message body, an AWS Step Functions input, a webhook that wraps its payload, or a JSON column exported to text.
- A test fixture or constant in source code, where the document must be a string literal in the language you write.
Stringifying turns {"a": 1} into "{\"a\":1}": quotes and backslashes inside the document are escaped and the whole thing is wrapped in quotes. With Minify first (on by default) the result equals what JSON.stringify(JSON.stringify(value)) produces, except that numbers are never re-parsed, so a 19-digit ID keeps every digit instead of being rounded. Turn it off to keep the document’s own line breaks, which then appear as \n escapes.
Language snippets
Output as picks the target. The escaping differs per language, and getting it wrong is a classic source of broken builds:
- JSON string literal: the plain
"…"value, ready to use inside another JSON document or an.envfile that accepts double quotes. - JavaScript: a single-quoted string, which keeps the many double quotes of JSON readable, plus
JSON.parse(json). - Python: a single-quoted literal passed to
json.loads. Control characters become\xHHescapes. - Java and C#: double-quoted literals for
Stringandstring. Characters a compiler rejects raw (control characters, and the separators U+0085, U+2028 and U+2029, which C# treats as line breaks) are written as\uXXXX, and so are lone surrogates, which a UTF-8 source file cannot store.
Each snippet is tested by decoding it back and comparing with the original document, so it pastes without edits.
Parsing a stringified string
Parse string does the reverse. It is most useful for logs and message queues where a payload was encoded twice, so you see "{\"order\":{\"id\":7}}" or worse, "\"{\\\"order\\\"…\"". The tool decodes the first level, checks whether the result is again a quoted string, and keeps going until it reaches something that is not, up to 32 levels. The list under the panes shows every level found; select one to see that intermediate text. When the final text is a JSON document it is pretty-printed with two-space indentation, keeping exact numbers. Untick Pretty-print decoded JSON to see it as it was.
The first level may also be escaped text without its quotes, as copied from the middle of a log line. Plain JSON that is not stringified at all is recognised too and simply reported as having no levels.
Errors
In Stringify mode the input must be valid JSON, so mistakes are reported where they are, with the line and column, before anything is generated. To wrap arbitrary text such as an SQL query or a log message instead, use JSON Escape, which accepts any text. In Parse mode, a broken first level is located the same way; deeper levels simply stop at the last text that decodes cleanly. For a quick structure check of the decoded result, open it in the JSON validator.
All work happens in a background worker in your browser, so even multi-megabyte payloads do not freeze the page and nothing is uploaded.
Examples
Config for an environment variable
The document is minified and wrapped as one JSON string, ready to paste as the value of an environment variable or a JSON field.
{
"featureFlags": { "newCheckout": true },
"retries": 3,
"region": "eu-west-1"
}"{\"featureFlags\":{\"newCheckout\":true},\"retries\":3,\"region\":\"eu-west-1\"}"Python test fixture
A single-quoted Python literal keeps the JSON readable; the apostrophe in the name is escaped for Python, not for JSON.
{
"name": "O'Brien",
"tags": ["vip", "beta"]
}Output as Pythonimport json
data = json.loads('{"name":"O\'Brien","tags":["vip","beta"]}')Double-encoded message from a queue
Two levels of stringification are peeled off, and the 20-digit id survives exactly in the pretty-printed result.
"\"{\\\"id\\\":12345678901234567890,\\\"tags\\\":[\\\"a\\\",\\\"b\\\"]}\""{
"id": 12345678901234567890,
"tags": [
"a",
"b"
]
}Common errors and how to fix them
| Error | Cause | Fix |
|---|---|---|
Trailing comma before '}'Explained | Stringify validates the input as strict JSON, and the document has a comma after its last property. | Remove the comma, or convert the file with JSONC to JSON first if it is a config with comments. |
This is neither a JSON string literal nor escaped text | In Parse mode the input is plain text without quotes or escapes, so there is no stringification to undo. | Paste the quoted string as it appears in the log or payload, or use Stringify mode instead. |
Invalid escape sequence '\x' in stringExplained | The first level contains an escape JSON does not allow, often because the value was escaped for another language. | Replace the escape with its JSON form, for example \u0041 instead of \x41. |
Frequently asked questions
Is this the same as JSON.stringify?
With Minify first on, the JSON string literal equals JSON.stringify(JSON.stringify(value)) for the same document, except that big numbers keep their digits instead of being rounded.
Why is my JSON stringified twice in the logs?
A logger or SDK called JSON.stringify on a value that was already a JSON string. Parse string removes as many levels as there are; fixing the double call at the source avoids the overhead.
Which quotes does the JavaScript snippet use?
Single quotes, so the double quotes inside the JSON need no escaping. The string is valid in every JavaScript engine and in TypeScript.
Can I stringify text that is not JSON?
Use JSON Escape for arbitrary text. Stringify checks the input is JSON so that it can minify it safely.