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 option | Use 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 quotes | Pasting 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:\\logsin 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.