Bad escaped character in JSON at position 74
Updated by John Marshall
JSON.parse throws this SyntaxError when a backslash inside a string is followed by a character that isn’t a JSON escape. JSON allows a backslash before ", \, /, b, f, n, r and t, and before u with four hex digits.
The position is the index of the character after the backslash, counting from 0. Python calls the same mistake Invalid \escape and points at the backslash.
Quick fix
- Go to the position in the message. The character there comes right after a backslash.
- If the backslash is part of the value, as in a Windows path or a regular expression, write it twice:
\\. - For
\', remove the backslash. For\x41, write\u0041.
Fix your JSON
Windows paths
This file stores a Windows path with one backslash between folders:
{
"robot": "TIDY-004",
"firmware": "2.0.0-rc.3",
"export_path": "C:\Users\leo\Downloads\TIDY-004.log"
}Chrome and Edge report Bad escaped character in JSON at position 74 (line 4 column 22), the U of Users. The \l, \D and \T later in the path are invalid as well, and only the first is reported. With all four doubled, the file parses:
{
"robot": "TIDY-004",
"firmware": "2.0.0-rc.3",
"export_path": "C:\\Users\\leo\\Downloads\\TIDY-004.log"
}JSON.parse turns each \\ into one backslash, so the value your code gets is C:\Users\leo\Downloads\TIDY-004.log.
A folder name that starts with a lowercase u, t, n, b, f or r behaves differently. "C:\users\leo" fails with Bad Unicode escape in JSON, because \u has to be followed by four hex digits. "C:\temp\new.log" parses without an error: \t and \n are valid escapes, so the value is C:, a tab, emp, a line break and ew.log.
Regular expressions, LaTeX, \' and \x
A regular expression kept in JSON has the same problem. {"id_pattern": "^TIDY-\d{3}$"} fails at position 23, the d. Written as "^TIDY-\\d{3}$", it parses and the value is ^TIDY-\d{3}$.
LaTeX commands start with a backslash too. "\sqrt{2}" fails at the s. "\frac{1}{2}" parses, because \f is the escape for a form feed, and the value is a form feed followed by rac{1}{2}.
\' and \x41 are escapes in JavaScript strings, and JSON has neither. PHP’s addslashes() puts a backslash before every single quote, so its output can’t go into JSON. Use json_encode().
JSON built by joining strings
A path held in a variable has single backslashes. Putting it between quotes by hand copies them into the JSON as they are. JSON.stringify doubles them:
// exportPath holds C:\Users\leo\Downloads\TIDY-004.log
const broken = '{"export_path": "' + exportPath + '"}';
JSON.parse(broken);
// SyntaxError: Bad escaped character in JSON at position 20
const text = JSON.stringify({ export_path: exportPath });
// {"export_path":"C:\\Users\\leo\\Downloads\\TIDY-004.log"}Python’s json.dumps does the same. To escape one value by hand, paste it into the JSON escape tool.
JSON typed into JavaScript source
A JavaScript string literal has its own backslash escapes, and JavaScript processes them before JSON.parse runs. In source code \\ is one backslash, so JSON that is escaped correctly loses one level:
JSON.parse('{"export_path": "C:\\Users\\leo"}');
// SyntaxError: Bad escaped character in JSON at position 20
const text = String.raw`{"export_path": "C:\\Users\\leo"}`;
console.log(JSON.parse(text).export_path);
// C:\Users\leoString.raw keeps the backslashes as typed. Without it, each backslash in the value takes four in the source: '{"export_path": "C:\\\\Users\\\\leo"}'. A Python string literal behaves the same way, and a raw string such as r'{"export_path": "C:\\Users\\leo"}' avoids it.
JavaScript drops a backslash it doesn’t recognise and raises no error. JSON.parse('{"id_pattern": "^TIDY-\d{3}$"}') succeeds, and the pattern comes out as ^TIDY-d{3}$.
JSON read from a file or a response reaches the parser as it is, so none of this applies to it.
A value that ends in a single backslash gives a different message, covered in Unterminated string in JSON.
The same error elsewhere
| Chrome, Edge | Bad escaped character in JSON at position 74 (line 4 column 22) |
|---|---|
| Node.js 20 | Bad escaped character in JSON at position 74 |
| Firefox | JSON.parse: bad escaped character at line 4 column 22 of the JSON data |
| Safari | JSON Parse error: Invalid escape character U |
| Python | json.decoder.JSONDecodeError: Invalid \escape: line 4 column 21 (char 73) |
| Go | invalid character 'U' in string escape code |
| jq | jq: parse error: Invalid escape at line 4, column 54 |
| PHP, json_last_error_msg() | Syntax error |