JSON to String

Validate a document, then hand it back as a quoted and escaped string literal ready to paste into your language of choice.

JSON
String literal
Runs locally in your browser

About the JSON to String

Test fixtures, migration scripts and quick reproductions all need a JSON document embedded in source code as a string. Doing that by hand means escaping every double quote and every backslash, and one missed character produces a compile error or, worse, a payload that silently differs from the original. This tool parses the JSON first, so you know it is valid, then escapes it once and wraps it for the target you pick.

Seven targets are offered. The raw option gives just the quoted string when you are pasting into a field or a test assertion. JavaScript, Java, C# and Python each get a complete declaration with the variable name you choose, sanitised to a legal identifier. PHP additionally escapes the dollar sign so that a value like $5 is not read as a variable inside a double quoted string. The shell target uses single quotes and the standard quote closing trick, so the value survives being passed to curl.

Minifying before quoting is on by default because embedded fixtures rarely benefit from indentation and every space costs two characters once escaped. Turn it off and pick an indent width when the literal will be read by a person, for example in a documentation snippet; the newlines then appear as \n escapes.

The reverse trip is JSON Unstringify, which strips the declaration and the escaping and gives the document back.

How to use

  1. Paste valid JSON. Invalid input is rejected with the line and column, so you never embed a broken fixture.
  2. Choose a Language and set the Variable name you want in the generated line.
  3. Leave Minify before quoting ticked for the shortest literal, or untick it to keep the indentation as \n escapes.
  4. Copy the line straight into your source file.

Common questions

Does the tool check that my JSON is valid first?
Yes. It parses the input before quoting anything, so a syntax error is reported with its line and column rather than baked into a string literal.
Why does the PHP output escape dollar signs?
Inside a double quoted PHP string a dollar sign starts a variable. Escaping it keeps the character literal and the JSON intact.
Can I get the string without a variable declaration?
Yes, choose the "Quoted string only" language. You get the quoted, escaped value and nothing else.
What is the difference between this and the JSON Escape tool?
JSON Escape escapes any text without checking it. This tool requires valid JSON, can minify it first, and adds the declaration syntax for a specific language.