Encode JSON as a string
What encoding JSON as a string changes, how escaping works, when double-encoded JSON is the right choice, and when to keep plain JSON for readability and editing.
Encoding JSON as a string usually means one of two things. The first is serializing a data structure into JSON text. The second is taking JSON text that already exists and escaping it so it can sit inside a string value, for example in a field of another JSON document. Both are lossless when done with a proper encoder. Both also change the actual characters, so the result is less pleasant to read and easier to break by hand. This review covers what changes, when it makes sense, and when plain JSON is the better choice.
Is encoding JSON as a string the right choice?
Use it when a destination needs a string and you have structured data. Common cases are an API field typed as a string that carries a JSON payload, a database column or environment variable that stores text, a log message, or a value embedded in source code. The main benefit is that the data survives transport intact, and a decoder can rebuild it exactly. The decisive tradeoff is readability. Every layer of encoding adds backslashes and escaped quotes, so the result is harder to scan, search and edit.
- Encoding as a string fits when the receiving system expects a string value and will decode it.
- Encoding fits when JSON must be nested inside other JSON, a code literal or a single-line log entry.
- Keep plain JSON when people will read, review or hand-edit the data.
- Keep plain JSON when the destination already accepts JSON objects directly. Wrapping it in a string only adds a decoding step on the other side.
- Keep plain JSON for documentation and examples, unless you are specifically showing the escaped form.
What the transformation actually changes
This is a change to the stored characters, not a visual effect. Nothing is reversed, mirrored, restyled or substituted with look-alike Unicode glyphs. No display trick such as CSS or bidirectional text controls is involved. The encoder adds quotation marks around the whole value and inserts escape sequences for characters that would otherwise end the string or are not allowed in it raw. A decoder removes exactly those additions. Because the encoder never reorders characters, combining marks and multi-code-point emoji keep their sequence and come back unchanged after decoding.
The example below shows both meanings of encoding in JavaScript. The record's note contains a real newline character. Serializing turns it into the two-character escape \n. Encoding that JSON text again escapes every quotation mark and backslash, so \n becomes \n. Parsing twice restores the original record.
const record = { name: "Ada", note: "Line 1\nLine 2" };
const jsonText = JSON.stringify(record);
// {"name":"Ada","note":"Line 1\nLine 2"}
const encoded = JSON.stringify(jsonText);
// "{\"name\":\"Ada\",\"note\":\"Line 1\\nLine 2\"}"
const decoded = JSON.parse(JSON.parse(encoded));
// deep-equal to record
The JSON standard requires certain characters to be escaped inside strings and allows a few others to be. The table summarizes what to expect in encoded output.
| Character in the value | How it appears in the encoded string |
| Quotation mark " | " |
| Backslash \ | \ |
| Line feed (newline) | \n |
| Carriage return | \r |
| Tab | \t |
| Backspace and form feed | \b and \f |
| Other control characters U+0000 to U+001F | \u followed by four hexadecimal digits, for example \u0001 |
| Forward slash / | Usually left unchanged; / is permitted but not required |
| Non-ASCII letters and emoji | Left as is, or written as \u escapes if the encoder is set to ASCII-only output |
The last row is where encoders differ most visibly. Python's json.dumps escapes non-ASCII characters by default. Characters outside the Basic Multilingual Plane, such as most emoji, become a surrogate pair of two \u escapes. JavaScript's JSON.stringify leaves those characters as they are. Either form decodes to the same text. The ASCII-only form is safer for channels that mishandle non-ASCII bytes, but a person cannot read it at a glance.
import json
record = {"name": "Zoë", "mood": "😀"}
print(json.dumps(record))
# {"name": "Zo\u00eb", "mood": "\ud83d\ude00"}
print(json.dumps(record, ensure_ascii=False))
# {"name": "Zoë", "mood": "😀"}
print(json.dumps(json.dumps(record, ensure_ascii=False), ensure_ascii=False))
# "{\"name\": \"Zoë\", \"mood\": \"😀\"}"
Where meaning can be lost: serialization, not escaping
Escaping a string is reversible. Turning program data into JSON is not always reversible, because JSON has fewer types than most languages. If the data started as an object in code rather than as JSON text, check these documented behaviors before trusting a round trip.
- JavaScript JSON.stringify drops properties whose values are undefined, functions or symbols. Inside arrays, those values become null.
- JavaScript turns NaN and Infinity into null, and throws a TypeError for BigInt values.
- JavaScript Date objects become ISO date strings and do not come back as Date objects when parsed.
- Python json.dumps writes NaN and Infinity by default. Those tokens are not valid JSON and strict parsers reject them. Passing allow_nan=False makes the encoder raise an error instead.
- Python converts non-string dictionary keys, such as integers, into strings, and converts tuples into arrays.
Not the same as other kinds of encoding
Searches for encoding JSON sometimes refer to a different operation. Each of the following produces a different output and needs its own decoder. Mixing them up is a common reason a payload fails to parse.
| Operation | What it does | Typical use |
| JSON string encoding | Wraps text in quotes and escapes quotes, backslashes and control characters | Nesting JSON inside a JSON string field or code literal |
| Base64 | Converts bytes into a restricted alphabet; output is not human-readable | Binary-safe transport, tokens, data embedded in headers |
| URL percent-encoding | Replaces reserved characters with % sequences | Placing JSON in a query string |
| HTML entity escaping | Replaces characters such as < and & with entities | Showing JSON inside an HTML page |
| Pretty-printing or minifying | Adds or removes whitespace between tokens; values unchanged | Readability versus compact size |
Will the result stay readable and usable after copying or sharing?
An encoded string survives copying unchanged as long as the destination treats it as plain text. The risks come from destinations that rewrite text. Some word processors and messaging apps replace straight quotation marks with typographic ones, and either JSON form stops parsing after that. Shells and programming languages add their own quoting rules, so pasting an encoded string into a command line or source file can need a further layer of escaping. Wrapping or line-breaking long single-line values may insert characters that were not in the original. Before pasting into a tool, check how it handles quotes and backslashes rather than assuming it behaves like the last one that worked.
Searching and editing are the bigger day-to-day costs. A search for a value containing quotes or newlines has to match the escaped form, not the readable one. Hand-editing a double-encoded string is error-prone because one missing backslash breaks the whole value. The dependable workflow is to decode, edit the plain JSON, validate it, then encode again with a library function rather than by hand.
For accessibility, escaped JSON is dense punctuation. How a screen reader announces backslashes, quotation marks and \u sequences depends on the product and its punctuation verbosity settings. No tested results for a specific assistive technology were available for this review. Whenever people, rather than programs, are the audience, provide the decoded plain JSON, ideally pretty-printed, alongside or instead of the encoded string.
Using online encoders with real data
Browser-based JSON-to-string converters are convenient for quick checks. Whether a given tool processes input locally or sends it to a server cannot be determined from its appearance or marketing. For anything containing credentials, personal data or private payloads, check the tool's own documentation, or use the built-in functions of your language, which run on your machine.
Gaps in this review
No product-specific evidence was supplied, so this review names no particular encoding tool and gives no performance or size measurements. The behaviors described come from the JSON standard and the documented behavior of JavaScript's JSON methods and Python's json module. How a particular editor, chat app or screen reader handles encoded output has not been tested here and should be verified in that environment.