How to Escape and Unescape JSON Strings (Quotes, Backslashes, Newlines & Unicode)
If you've ever hit Unexpected token in JSON at position 42 or Unterminated string , there's a good chance an unescaped character broke your payload. JSON is strict about what's allowed inside a string, and the fix is almost always escaping . Here's the practical version. What does escaping a JSON string mean? A JSON string is wrapped in double quotes. Any character that would confuse the parser must be replaced with a backslash escape sequence. Escaping doesn't change the meaning of your text — it just makes the string valid JSON so parsers can read it. Unescaping is the reverse: turning those sequences back into readable characters (handy when you copy a value out of logs or an API response). The characters you must escape JSON defines exactly seven characters that must be escaped inside a string: Character Escaped as Double quote " \" Backslash \ \\ Newline \n Carriage return \r Tab \t Backspace \b Form feed \f The forward slash / may optionally be escaped as \/ , but it isn't required. Unicode can be written as \uXXXX (four hex digits). JSON escape examples Double quotes — He said "hello" becomes: "He said \" hello \" " Backslashes (Windows paths) — C:\temp\file.txt becomes: "C: \\ temp \\ file.txt" Newlines and tabs — a two-line, tabbed string becomes: "Line 1 \n Line 2 \t Tabbed" How to escape and unescape JSON in code In production you rarely escape by hand — every language has it built in. JavaScript const escaped = JSON . stringify ( text ); // escape const back = JSON . parse ( escaped ); // unescape Python import json escaped = json . dumps ( text ) # escape back = json . loads ( escaped ) # unescape Java (Jackson) ObjectMapper mapper = new ObjectMapper (); String escaped = mapper . writeValueAsString ( text ); String back = mapper . readValue ( escaped , String . class ); C# (.NET) using System.Text.Json ; string escaped = JsonSerializer . Serialize ( text ); string back = JsonSerializer . Deserialize < string >( escaped ); Common escaping mistakes (and f