Why Your JSON Is Full of Backslashes: Escaped and Double-Encoded JSON
Where strings like "{\"id\":1}" come from, how to unescape them safely in JavaScript, Python and jq, and how to find the extra JSON.stringify that caused them.
What you are looking at
You expected an object and got this instead:
"{\"id\":42,\"status\":\"paid\",\"items\":[{\"sku\":\"A-1\"}]}"This is valid JSON — but it is a single JSON string, not an object. Somewhere, an object was converted to JSON text, and that text was then serialized again as a string value. Every " inside became \" so that the outer quotes still delimit one string.
Code that reads response.status gets undefined, because the response is a string. Code that calls JSON.parse once gets the inner JSON text, still a string. You need to decode one more time.
The escape sequences you will see
| Sequence | Means |
|---|---|
\" | A double quote inside the string |
\\ | A literal backslash |
\n | Newline (also \r carriage return, \t tab) |
\/ | A forward slash (optional escape, common in PHP output) |
\u00e9 | A Unicode code point, here é |
\\\" | An escaped quote that was escaped again: a sign of double encoding |
When you see \\\" or \\\\, the value has been stringified at least twice and needs to be decoded the same number of times.
Where double encoding usually comes from
- Stringifying before a library that stringifies for you.
res.json(JSON.stringify(data))in Express,Response.json(JSON.stringify(data))in fetch-style handlers, orrequests.post(url, json=json.dumps(data))in Python. - JSON stored in a text column. The database returns the column as a string, and the API serializes the whole row, so the column value arrives escaped.
- Envelopes that carry the payload as a string. AWS API Gateway passes the request body to Lambda as
event.body(a string); SNS messages delivered to SQS carry the original message in a string field; many webhook providers sign and send a stringified payload. - Structured loggers. Logging
JSON.stringify(obj)as the message of a JSON logger escapes it again inside the log line.
How to unescape it
The safe way is always to parse, never to search-and-replace. Parse until you reach a real value:
function parseDeep(text, maxLayers = 5) {
let value = JSON.parse(text);
for (let i = 0; i < maxLayers && typeof value === 'string'; i++) {
try { value = JSON.parse(value); } catch { break; } // a plain string, not JSON
}
return value;
}import json
value = json.loads(raw)
while isinstance(value, str):
try:
value = json.loads(value)
except json.JSONDecodeError:
break# input: "{\"id\":42}"
jq 'fromjson' payload.json
# a field that contains a JSON string
jq '.body | fromjson' event.jsonFor a one-off value copied from a log, paste it into the JSON Unstringify tool: it decodes every layer and pretty-prints the result.
Why replacing \" with " is a bad idea
A regular expression that strips backslashes works on simple samples and corrupts real data. If a value legitimately contains a quote or a backslash — a Windows path, a regex, a quoted product name — removing escapes produces invalid JSON, or valid JSON with different content.
"{\"path\":\"C:\\\\temp\",\"title\":\"The \\\"best\\\" deal\"}"
// stripping every backslash breaks both the path and the inner quotesA JSON parser knows which backslashes belong to which layer. A regex does not.
Fix it at the source
Unescaping on the consumer side is a workaround. The real fix is to serialize exactly once, at the boundary:
app.get('/order', (req, res) => {
res.json(JSON.stringify(order)); // string serialized again
});app.get('/order', (req, res) => {
res.json(order); // res.json serializes the object once
});- Find the first place where the payload is a string instead of an object (log typeof value at each hop).
- Look for an explicit JSON.stringify / json.dumps right before a library call that already serializes.
- If a string envelope is part of a third-party contract (Lambda, SNS, webhooks), parse it once at the entry point and pass objects inward.
- Add a test that asserts the response body is an object, not a string.
When an escaped string is exactly what you want
Sometimes a contract really requires JSON inside a string: a metadata field that only accepts text, an environment variable, a fixture embedded in source code. In those cases stringify deliberately, document that the field contains JSON text, and decode it with a single extra parse on the reading side. The JSON Stringify tool produces that escaped form from any JSON document.