How to Escape JSON for Code, CLI & APIs
When embedding JSON payloads into software source code (such as unit tests in Java, C#, Python, or JavaScript), or passing JSON data through terminal commands like curl -d, internal double quotes (") must be escaped with a preceding backslash (\"). Without proper escaping, string interpreters encounter premature quote terminations, resulting in syntax errors. TinyTools JSON Escape turns any JSON object into an escaped string literal tailored for your target environment.
How to Escape JSON Online
- Input JSON: Paste your JSON object into the left code editor or click Sample.
- Select Language Preset: Choose your target syntax (Generic, JavaScript, Java/C#, Python, or cURL Command).
- Toggle Options: Check Minify before escaping to compact the string into a single line, and Wrap in outer quotes if your code requires wrapping quotes.
- Copy String: Click Copy String and paste the escaped literal directly into your code editor or terminal.
Code Example: Raw JSON to Escaped Java / C# String
Consider this standard JSON object:
{
"user": "Sarah Connor",
"active": true
}
When escaped for Java or C# string literals, all internal quotes are escaped with backslashes and wrapped in double quotes:
"{\"user\":\"Sarah Connor\",\"active\":true}"
Supported Presets & Environments
JavaScript & TypeScript
Generates standard JSON-compliant string literals ready for JSON.parse("...") expressions.
Java & C#
Escapes quotes and newlines for String json = "..."; variables in Spring Boot, ASP.NET, and NUnit tests.
Python Strings
Escapes internal quotes for single-quoted Python strings ('{"a": 1}') or double-quoted literals.
cURL Command Line Payloads
Generates ready-to-run curl -X POST -d '...' commands formatted for Unix Bash or Windows Command Prompt.