DevKitHub

JSON & Data

JSON to String and String to JSON — Stringify and Unescape

Paste JSON to get it as a string literal ready to embed in code, a config value or an environment variable. Switch to String → JSON to unescape a stringified string, however many times it was quoted, and read it as formatted JSON.

6 lines
1 line

String literal

SizeUTF-8
116 bytes, minified

This tool runs entirely in your browser. Your input is never uploaded, stored or logged.

How it works

In the JSON → String direction the input is parsed first, so invalid JSON stops with its line and column instead of being escaped into a broken literal. It is then minified, with numbers and key order kept exactly as written, where JSON.parse would round an integer past 2^53 and move a key such as "1" ahead of "b". Last comes the escaping, which happens twice over: JSON’s own escapes, such as \" and \n, stay in the text, and the target language’s escaping goes on top, so every one of those backslashes is doubled. For a JSON string the result is exactly what JSON.stringify gives for the minified text. Unlike the text escape tool, which escapes any text, this one only accepts JSON, keeps it valid, and knows the traps of each place the literal is going.

For JavaScript, single quotes are the default because JSON is full of double quotes; backticks keep line breaks when formatting is kept, and ${ is escaped. U+2028 and U+2029 are escaped because a string literal could not contain them before ES2019, and "<" is written as \x3C before !--, script and /script, as the HTML Standard recommends, so the literal is safe inside a script element. Java translates \u escapes before it reads the source, so an undoubled \u000a is a real line break and a compile error. C# regular strings cannot hold U+0085, U+2028 or U+2029, so those are escaped. A .env value is written in single quotes. dotenv and a POSIX shell read everything between them literally, with no way to escape a quote there, while python-dotenv turns \\ into \ and expands ${…} inside them too. So apostrophes, escaped backslashes and dollar signs inside JSON strings become \u0027, \u005c and \u0024, which JSON reads as the same characters. Every target but C# was checked by decoding its output with the real parser.

In the String → JSON direction the input can be a JSON string in double quotes, a single-quoted or backtick literal, a C# verbatim @"…" string, a NAME='…' line from a .env file, which is read literally as dotenv reads it, or a line such as const s = "…"; copied from code. The escapes that JSON, JavaScript, Python, Java and C# share are decoded, and the process repeats while the result is itself a quoted string, so JSON that was stringified twice, as logs and queues often print it, comes out in one step, with the layer count reported. Escaped JSON with its outer quotes already stripped, such as {\"a\":1}, is recognised too. An error carries the line and column in the text you pasted, traced back through every layer, rather than a position in an intermediate string you never saw.

Common problems

Every example below is run against this tool in our test suite, so what it says here is what the tool actually does.

After removing 1 layer of quoting, the text is not valid JSON: A JSON string cannot contain a raw line break; write it as \n.

'{"note":"line one\nline two"}'
Why:
The literal was written by pasting JSON between quotes without escaping it again, so the language turned JSON’s own \n into a real line break, which a JSON string cannot contain.
Fix:
Double every backslash when embedding JSON in a literal (\\n), or generate the literal with JSON → String.

An unescaped " ends the string early. If this is JSON that was wrapped in quotes without being escaped, remove the outer quotes.

"{"a":1}"
Why:
The text was wrapped in quotes but its own quotes were never escaped, which is how many loggers print a string value. It is neither JSON nor a string literal.
Fix:
Remove the outer quotes and paste the JSON on its own, or use JSON → String to write a correct literal.

After removing 1 layer of quoting, the text is not valid JSON: A JSON string cannot contain a raw control character; escape it, for example as \t.

"{\"dir\":\"C:\temp\"}"
Why:
A Windows path was written with single backslashes. \t is a valid escape in every one of these languages, so C:\temp became C:, a tab and emp before the JSON was even read.
Fix:
Write C:\\\\temp in the literal: that is C:\\temp in the JSON, and C:\temp in the value.

\d is not an escape in JSON, JavaScript, Python, Java or C#.

"{\"pattern\":\"\d+\"}"
Why:
A regular expression was written with single backslashes. JavaScript reads "\d" as a plain d, Python keeps the backslash and warns, and javac refuses to compile it, so rather than pick one reading the decoder stops.
Fix:
Write \\\\d in the literal, which is \\d in the JSON and \d in the pattern.

Stringifying the text added another layer of backslashes instead of removing one.

Why:
The input was already a JSON string, such as "{\"a\":1}". JSON → String treats it as a string value and quotes it again, which is what JSON.stringify does as well.
Fix:
Switch to String → JSON to remove layers. JSON → String warns when its input is already a string.

javac reports “unclosed string literal” for JSON that contains \u000a.

Why:
Java translates \u escapes before it tokenises the source, so \u000a pasted into a string literal unchanged is a real line break inside it.
Fix:
Double the backslash, which the Java target always does, so JSON’s escape reaches the JSON parser intact.

Frequently asked questions

How do I convert a string to JSON in JavaScript?
JSON.parse(text). If the result is still a string, the JSON was stringified twice, and a second JSON.parse gives the object. String → JSON does the same thing for any number of layers and tells you how many it removed.
Is this the same as JSON.stringify?
For the JSON string target, yes: the output is JSON.stringify of the minified JSON text. The other targets start from the same text and escape it for the language instead, because a JSON string literal is not always a valid literal elsewhere: Java translates \u escapes before compiling, and a .env file has no escapes at all inside single quotes.
How do I put JSON in an environment variable?
Minify it and wrap it in single quotes: APP_CONFIG='{"debug":false}'. dotenv, python-dotenv and a shell all read that literally. Apostrophes, dollar signs and escaped backslashes inside the JSON need rewriting as \u escapes first, which the .env target does.
Why does my JSON have backslashes before every quote?
Because it has been turned into a string, usually by a logger or a message queue serialising a value that was already JSON. Each layer of stringification escapes the quotes and backslashes of the layer inside it. Paste it into String → JSON to peel the layers off.

Last updated