PRACTICAL GUIDE

Escape Text for a JSON String Value

Convert plain text into a JSON string literal or decode a quoted JSON string back into plain text.

Choose the right syntax

JSON string escaping protects double quotes, backslashes and control characters inside a JSON value. It is different from HTML or URL encoding. Choose the encoding for the place where the value will be used, rather than repeatedly applying unrelated encoders.

Keep the surrounding quotes

The escaped result is a complete string literal with double quotes. Unescape expects that complete representation. Pasting bare text or a JSON object produces an error because those inputs are not the string value this tool is designed to decode.

Distinguish a newline from its escape

A literal newline and the two characters backslash followed by n are different inputs. Escaping preserves that distinction. Copy the entire result when moving it into JSON, and avoid having another editor add an extra escaping layer.

Serialize complete documents properly

When building an API payload in code, use the language’s JSON serializer for the whole object. Manually joining quoted fragments can introduce mistakes. This tool is useful for inspecting a string value, not replacing structured serialization throughout an application.

Put it into practice