How to Parse JSON with Escape Characters in JavaScript

U

Utilx Team

July 6, 2026

When working with APIs, databases, or configuration files, you will inevitably run into JSON strings that contain escape characters—such as \" for double quotes, \\ for backslashes, or \n for newlines.

If you try to parse these strings directly in JavaScript using JSON.parse(), you will often be met with the infamous error: SyntaxError: Unexpected token ... in JSON at position ...

This guide explains why this error happens, how to solve it in your JavaScript code, and how to use the Utilx JSON Formatter to format and validate escaped JSON instantly without writing a single line of code.


🎯 Quick Answer: How to Parse Escaped JSON

To parse a JSON string containing escape characters in JavaScript, you must ensure the escape characters are themselves properly escaped (double-escaped) before passing them to JSON.parse().

// ❌ Throws SyntaxError because the backslash is treated as a JS string escape, not a JSON escape
const raw = '{"message": "Hello \"world\""}';
JSON.parse(raw); 

//  Double-escape the backslashes so they are passed correctly to the parser
const escaped = '{"message": "Hello \\"world\\""}';
const parsed = JSON.parse(escaped);
console.log(parsed.message); // Output: Hello "world"

If you have a malformed or heavily escaped JSON string and just want to clean it up instantly, paste it into the Utilx JSON Formatter. It automatically detects, unescapes, and beautifies invalid JSON strings directly in your browser.


Understanding the Problem: Why JavaScript Fails to Parse Escaped JSON

JSON syntax requires strings to be wrapped in double quotes. If a string value contains double quotes or backslashes, they must be escaped with a backslash (\).

The confusion arises because JavaScript strings also use the backslash as an escape character.

When you write:

const str = '{"file": "C:\users\data"}';

JavaScript interprets \u as the start of a Unicode escape sequence. Since \u is followed by se (which is not a valid hex code), the string construction fails or gets mangled before it even reaches the JSON.parse() parser.

Common Escape Characters in JSON:

  • \" - Double quotation mark
  • \\ - Backslash
  • \/ - Forward slash
  • \n - Newline
  • \r - Carriage return
  • \t - Tab

How to Resolve JSON Escape Errors Programmatically

Here are the three most common ways to handle escaped JSON in JavaScript.

Method 1: Double-Escaping (For Hardcoded Strings)

If you are writing the JSON string manually in your JavaScript code, you must use a double backslash (\\). The first backslash escapes the second backslash in JavaScript, ensuring a single backslash is passed to the JSON engine.

// Correct way to write escaped JSON in code:
const jsonString = '{"path": "C:\\\\users\\\\data.json", "quote": "She said \\"hello\\""}';
const parsed = JSON.parse(jsonString);

Method 2: Replacing Single Backslashes (For Dynamic Strings)

If you are receiving raw text from an input or file where backslashes are unescaped, you can use a regular expression to clean up the string before parsing.

function parseEscapedJson(rawString) {
  try {
    // Replace raw backslashes with double backslashes, unless they are already escaping a valid character
    const cleaned = rawString.replace(/\\(?!["\\/bfnrtu])/g, '\\\\');
    return JSON.parse(cleaned);
  } catch (error) {
    console.error("Failed to parse JSON:", error.message);
    return null;
  }
}

Format & Fix Escaped JSON Instantly with Utilx

Instead of writing custom regular expressions to debug API responses, you can use the Utilx JSON Formatter.

Designed to make data workflows seamless, it handles malformed and escaped JSON automatically:

  1. Open the Tool: Go to the JSON Formatter.
  2. Paste Your Escaped JSON: Paste the raw text—even if it is a single-line string with escaped double quotes or unescaped backslashes.
  3. Automatic Repair & Formatting: The tool detects common escaping errors, highlights the broken syntax, and formats the output into clean, indented JSON.
  4. Export: Copy the clean JSON or download it as a .json file for your codebase.

Why use Utilx?

  • Privacy-First: Your code is parsed 100% locally in your browser. Sensitive database strings or customer credentials never touch our servers.
  • Detailed Diagnostics: If your JSON is completely unparseable, Utilx highlights the exact line and character causing the syntax error.
  • Convert to Typescript: Instantly generate TypeScript interfaces or JSON schemas from your parsed JSON object.

Conclusion

Understanding JavaScript's double-escaping requirement is key to fixing JSON.parse errors. For production code, make sure your backend serialize library handles escaping correctly. For debugging, save time by dropping your raw text into the Utilx JSON Formatter to clean it up in seconds.

[Suggested internal link: /json/formatter] [Suggested internal link: /developer/regex-tester] [Suggested internal link: /developer/base64-encode]

U

About the author

Utilx Team

The engineering team behind Utilx — building privacy-first developer utilities that run entirely in the browser.

JSON Suite

JSON Formatter

Securely format, beautify, and validate JSON data instantly in your browser.

Launch Tool
Bespoke Development

Need a Customized Tool?

We build private, localized utilities tailored to your business logic.

Email our Team
Direct Contact Protocolutilxapp@gmail.com