FormatterHQ

JSON to String Converter

Convert a JSON payload into a quoted, escaped string you can drop straight into source code, unit tests or request fixtures, with the quote style your language expects.

Input JSON
Output TEXT
Ready.

How to use the JSON to String Converter

  1. Paste the JSON you want to embed, or click Sample to load a pretty-printed example.
  2. Pick the Quotes style that matches the language you are pasting into.
  3. Tick Keep line breaks if the string should keep the indented layout.
  4. Click Convert to String, then Copy the literal into your code.

JSON to string for source code

This JSON to string converter prepares a JSON payload for life inside a program. Test fixtures, mocked API responses, request bodies in integration tests and default settings often need a JSON document written as a string literal, and escaping every quote by hand is slow and error-prone.

Paste formatted JSON and you get one escaped line that compiles in the language of your choice.

Example: a request body for a test

{"query": "status:open", "limit": 20, "tags": ["bug", "ui"]}

With Quotes set to Double, the output is a literal that works in Java, C#, Go, JavaScript and Python:

"{\"query\":\"status:open\",\"limit\":20,\"tags\":[\"bug\",\"ui\"]}"

In a Java test that becomes:

String body = "{\"query\":\"status:open\",\"limit\":20,\"tags\":[\"bug\",\"ui\"]}";

With Single quotes the inner double quotes stay as they are, which is much easier to read in JavaScript, TypeScript, PHP or Python:

const body = '{"query":"status:open","limit":20,"tags":["bug","ui"]}';

Which quote style fits which language

Quotes optionUse it in
Double "..."Java, C#, Go, C/C++, Kotlin, Swift, JSON values, and any language where only double quotes form strings
Single '...'JavaScript, TypeScript, PHP, Python, Ruby; apostrophes inside values are escaped as \'
No outer quotesPasting between quotes you have already typed, or into a template that adds its own

Keep line breaks

By default the JSON is compacted first, which keeps the literal on one line. Tick Keep line breaks when the string will be shown to people, for example as the default content of a text area or in a log message; the result keeps a 2-space layout encoded as \n escapes. The value still parses to the same data either way.

Things to watch for

  • Backslashes in your data are doubled, so a path such as C:\\logs in JSON appears with four backslashes in the literal. That is expected.
  • Invalid JSON is escaped as plain text instead of being rejected, and the status bar says so. Check that message if you expected a compact result.
  • For multi-line text that is not JSON, such as SQL or an email template, the JSON multiline string converter gives more control over line handling.

To go the other way, paste a literal into String to JSON. If you only need a single value escaped for a Java string, the Java escape tool works on plain text.

Privacy

The conversion runs locally in your browser, so API keys or customer data inside a fixture are not uploaded. Only Load URL and Save & Share involve our server.

Convert JSON to a string in Python

Use json.dumps() to turn a dictionary or list into a JSON string. The mistake to avoid is str(data): it returns Python's own notation with single quotes, False and None, which json.loads() and every other JSON parser reject. json.dumps() writes double quotes, false and null, and json.loads() turns the result back into the same data. When you need the JSON as an escaped literal, to paste into source code or to store inside another JSON value, call json.dumps() once more on the string.

import json

data = {"query": "status:open", "limit": 20, "tags": ["bug", "ui"], "archived": False, "owner": None}

print(str(data))   # Python repr, not JSON
# {'query': 'status:open', 'limit': 20, 'tags': ['bug', 'ui'], 'archived': False, 'owner': None}

text = json.dumps(data, separators=(",", ":"))   # a JSON string
print(text)
# {"query":"status:open","limit":20,"tags":["bug","ui"],"archived":false,"owner":null}
print(type(text).__name__, json.loads(text) == data)   # str True

# Escaped literal for pasting into source code or another JSON value
print(json.dumps(text))
# "{\"query\":\"status:open\",\"limit\":20,\"tags\":[\"bug\",\"ui\"],\"archived\":false,\"owner\":null}"

The last line printed is what this page returns with Quotes set to Double, so paste a document above whenever you want to check a literal by eye.

Frequently asked questions

How do I put JSON in a Java or C# string?
Choose Double quotes and convert. Every inner double quote is escaped with a backslash, so the output can be assigned directly to a String variable. For C# you can also paste it into a regular (non-verbatim) string.
Can I use the output in Python?
Yes. Both the Double and Single styles are valid Python string literals, and json.loads() on the resulting string returns the original data. Single quotes are usually easier to read.
Why is my JSON compacted to one line?
Compacting keeps the literal short and avoids line breaks in your source file. If you want the indentation preserved, tick Keep line breaks and the layout is encoded with \n escapes inside the string.
Does this change my data?
No. Only whitespace between tokens is removed and the text is escaped. Decoding the string gives back exactly the same keys, values and order.

Last updated October 3, 2026

Related tools

JSON to Query String Build URL-encoded query strings from JSON objects, with a choice of bracket, indexed or repeated keys for arrays. Query String to JSON Parse URL query strings into JSON, including repeated keys, PHP/Rails bracket arrays (a[]=1) and nested objects (a[b][c]=1). String to JSON Converter Convert an escaped JSON string back into formatted JSON. Handles quoted literals, backslash-escaped text and double-encoded JSON. XML to String Converter Convert XML to an escaped string literal for Java, C#, JavaScript or JSON. Minify first or keep lines as concatenated strings. JSON Multiline String Converter Convert multi-line text into a valid JSON string with \n escapes, a JSON array of lines, or concatenated string literals. Reverse String Reverse text by characters, words or lines, or flip the letters of each word individually. JSON Tree Viewer Display JSON as an expandable tree. Collapse and expand objects and arrays to understand deeply nested data structures. JSON Unflatten Rebuild nested JSON from flat dot-notation keys (a.b.c) or bracket paths (a[0].b), the reverse of JSON flatten. JSON Validator Validate JSON online and find syntax errors fast. Get the exact line and column of each problem and fix broken JSON in one click. JSON Viewer & Beautifier View JSON as a collapsible tree and beautify it at the same time. Explore nested objects and arrays without losing your place. JSON5 Formatter Format and beautify JSON5 with comments, unquoted keys and trailing commas, or convert JSON5 to strict JSON. JSON5 Validator Validate JSON5 syntax online. Check config files with comments and trailing commas and get line and column for each error.