JSON Stringify Online — Escape JSON for Code

Escape a JSON document into a string literal for embedding in JavaScript, Java, Python, or shell source. Or unescape a stored JSON string back to formatted JSON.

What is JSON stringify online?

A JSON stringify online tool escapes a JSON document into a single quoted string literal you can paste directly into source code. The output is valid as a JavaScript, TypeScript, Java, C#, Go, Rust, or Python (double-quoted) string — every internal quote is backslash-escaped, every newline becomes \\n, and every control character is replaced with the proper escape sequence. The result is what you'd get from JSON.stringify(JSON.stringify(obj)) in Node.

This tool is the round-trip companion to JSON Formatter. Use the JSON → String mode when you need a JSON payload as a literal in source code or a database column. Use the String → JSON mode to reverse the operation: paste an escaped string from a log line, a database row, or a serialised webhook and get back nicely-formatted JSON.

How to escape JSON for source code — 4 steps

  1. Choose a mode. Pick JSON → String to escape, or String → JSON to reverse and pretty-print an already-escaped string.
  2. Paste the input. Drop your JSON (or escaped string) into the Input panel. Load Sample shows a small object with embedded quotes that demonstrates the escaping rules.
  3. Click Convert. The tool runs JSON.stringify (or JSON.parse) in your browser and shows the result.
  4. Copy and embed. Click Copy and paste the escaped string between two double-quotes in your source file, test fixture, or database value.

Sample JSON and escaped string output

Input JSON

{
  "user": "alice",
  "message": "She said \"hi\" to me",
  "tags": ["a", "b"],
  "active": true
}

Output (string literal)

"{\"user\":\"alice\",\"message\":\"She said \\\"hi\\\" to me\",\"tags\":[\"a\",\"b\"],\"active\":true}"

Source-Code Ready

Output is a valid string literal in JavaScript, TypeScript, Java, C#, Go, Rust, and double-quoted Python — paste between two quotes and your compiler accepts it.

Round-Trip Mode

A second mode reverses the operation: paste an escaped string from logs or database, get back pretty JSON. The same tool covers both directions.

Client-Side Only

Stringify and unescape both run in your browser. JSON with API keys, secrets, or PII never leaves your device.

Common use cases

  • check_circleEmbedding sample JSON payloads as string literals in unit-test assertions and golden fixtures
  • check_circleStoring a JSON payload in a TEXT or VARCHAR column in databases that lack a native JSON type
  • check_circlePassing JSON through environment variables, CLI arguments, or CI/CD secrets that only accept strings
  • check_circleBuilding HTML data-* attributes or inline <script type="application/json"> bodies that must be a single string
  • check_circleEncoding JSON for safe inclusion in URL query parameters or HTTP form bodies
  • check_circleProducing inline JSON literals for snapshot tests, contract tests, and mocked API fixtures
  • check_circleReversing escaped JSON pulled from log lines, error messages, or third-party webhook payloads
  • check_circleQuickly inspecting a JSON-string column from a database UI without writing parser code

JSON stringify vs JSON encode: are they the same thing?

Mostly yes — JSON stringify, JSON encode, JSON serialise, and escape JSON all describe turning a JSON object into a string. The subtle distinction this tool draws is whether you want one level of stringification (the result is parseable JSON, e.g. {"a":1}) or two levels (the result is a quoted string literal containing escaped JSON, e.g. "{\"a\":1}"). The first is what an HTTP body needs; the second is what a source-code literal or a string-typed database column needs. JSON → String mode does the second.

Need to format or validate the JSON first?

Pretty-print, validate, or convert your JSON before stringifying — all in your browser.

Frequently Asked Questions

What's the difference between JSON.stringify and stringify-for-source?

JSON.stringify(obj) produces a JSON document — a string that another machine can JSON.parse. Stringify-for-source goes one step further: it wraps that JSON document in quotes and escapes the inner quotes, producing a string literal that can be pasted directly into JavaScript, Java, or Python source. So {"a":1} becomes "{\"a\":1}" — a literal string token your compiler will accept.

Why escape JSON?

Three common reasons. First, embedding sample JSON inside a unit test assertion (assert response == "..."). Second, storing a JSON payload as a string column in a database that does not have a native JSON type. Third, passing JSON through a system that only accepts strings — URL query params, environment variables, shell arguments, HTML data-* attributes. Each of these requires the JSON to be a single, escaped string token.

What characters get escaped?

The standard JSON escape sequences: double-quote (\"), backslash (\\), forward-slash (optionally \/), backspace (\b), form-feed (\f), newline (\n), carriage return (\r), tab (\t), and any control char or non-BMP code point as \uXXXX. The output matches what JSON.stringify produces in JavaScript and json.dumps produces in Python.

Can I reverse it — string back to JSON?

Yes. Switch the mode toggle to String → JSON, paste the escaped string (with the surrounding quotes), and the tool unescapes and re-formats it as pretty JSON. Useful for reading a JSON column you fetched from a database where the value is stored as an escaped string.

Why does my output have backslashes everywhere?

That is the point — it is a string literal. Every double-quote in your JSON is preceded by a backslash so the surrounding quotes don't terminate the string early. Paste the result between two quotes in your source file and the compiler will interpret the escapes back to a regular JSON string at runtime.

Does the output work in every language?

The escapes used (\", \\, \n, \t, \uXXXX) are accepted by JavaScript, TypeScript, Java, C#, Go, Rust, Python (with double-quoted strings), JSON itself, and most shell here-docs. Single-quoted Python strings and shell single-quoted strings have different rules — convert the surrounding quotes if you target those.

Will this work for embedding JSON in HTML?

For HTML data-* attributes or inline JSON-LD scripts, you also need to handle < (which can break script tags) and & (HTML entity start). The plain JSON escape produced by this tool is correct for JavaScript string embedding but for HTML you should additionally HTML-encode angle brackets and ampersands.

Is my JSON sent to a server?

No. Stringify and unescape both run entirely in your browser using JavaScript. JSON containing API keys, secrets, or proprietary payloads never leaves your device. Open DevTools Network tab and verify — no request fires when you click Convert.

JSON Stringify Online — Escape JSON for Code | OpenFormatter