Skip to main content

Guides

How to Sanitize Copied Text for Code, JSON & CSV

CleanPastedText editorial · Updated

Text workbench

Try it on your text

Cleaning mode

Removes hidden characters and standardizes AI-style punctuation.

Try a real example

Each sample contains a problem you cannot see.

Original

Pasted text

0 chars · 0 words

Cleaned

Ready to copy

0 changes

Text stays in this browser

Same words · No AI rewriting · No content logging

Why copied text can fail even when it looks correct

Software compares code points, not appearances. A pasted value can look ordinary while containing a zero-width space, a non-breaking space, a directional control, a different normalization form, or a lookalike letter from another script. Those differences can affect parsing, equality, search, sorting, identifiers, imports, and code review.

The Unicode Consortium treats source code as a special environment because the text a machine interprets can differ from what a reviewer perceives. Its Unicode Source Code Handling standard covers invisible characters, bidirectional display, lookalike glyphs, line endings, tooling, and diagnostics. A sanitizer is one useful inspection layer, but language-aware tools must make the final decision.

SymptomPossible character causeNext check
Two visible strings compare as unequalZero-width character or different normalization formInspect code points and field normalization policy
JSON fails near an apparently blank gapNBSP or another non-JSON structural spaceClean, then run a standards-compliant JSON parser
CSV numbers import as textSpecial spaces, control characters, or localized punctuationPreview code points, delimiter, encoding, and column types
Code appears reordered or a diff looks emptyBidirectional controls or invisible format charactersUse editor reveal mode, compiler diagnostics, and a security scanner
Lookup, deduplication, or search misses a valueSpecial spaces, soft hyphens, joiners, or canonical differencesDefine separate display and comparison representations

How to sanitize copied text safely

  1. 1. Preserve the original input

    Save the raw snippet or data file and record where it came from. This is essential for debugging and auditability, and it lets you restore legitimate characters if a broad cleanup policy changes more than the target allows.

  2. 2. Detect before you normalize

    Paste a copy into the cleaner above. CleanPastedText scans the untouched input before normalization, so a narrow no-break space (U+202F), zero-width space (U+200B), word joiner (U+2060), byte order mark (U+FEFF), or directional control remains visible in the report even if a later pass removes or folds it.

  3. 3. Choose a policy for the target, not the source

    For an ordinary copied snippet, start with AI Clean and inspect the result. Use Plain ASCII only when the destination contract explicitly permits ASCII and nothing else. For multilingual content, do not remove join controls blindly: U+200C and U+200D can be meaningful inside words. For database imports, define a field-level policy instead of applying one transformation to identifiers, display names, prose, and secrets alike.

  4. 4. Inspect every change

    Read the code-point report and compare output with input. A control character removed from the boundary of a CSV field is probably cleanup; a join control removed from a Persian name may be data loss. The report is designed to support that decision, not make it for you.

  5. 5. Run the destination's native validation

    Parse JSON, preview the CSV import, compile and test code, validate the form schema, or stage the database migration. Then use formatters, linters, repository security checks, allowlists, and constraints appropriate to the system. A character cleaner can make hidden baggage inspectable; only the target knows what is valid.

Recommended sanitation policy by use case

Use caseCleaner roleRequired follow-up
Copied code or shell commandsReveal and remove invisible or directional controlsRead commands, lint, compile, test, and review the diff
JSON or configurationNormalize obvious copy-paste characters and spacesParse against the expected schema; inspect escaping and values
CSV or spreadsheet importExpose NBSP, controls, odd line separators, and trailing spacesConfirm encoding, delimiter, quoting, locale, and column types
Forms and search keysCreate a predictable comparison representationUse field-specific allowlists, length limits, and server validation
Database text fieldsSupport an explicit ingestion policyPreserve raw values where needed and test round-trip behavior

What normalization can and cannot solve

Unicode normalization maps equivalent or compatibility-related sequences into a more consistent representation. That helps with comparisons and interoperability, but it is not a universal sanitization policy. The Unicode Normalization Forms specification distinguishes canonical normalization from compatibility normalization and notes that compatibility forms can remove distinctions.

CleanPastedText uses NFKC after its detection pass. This can fold fullwidth letters, ligatures, and other compatibility variants into simpler forms. It does not make every lookalike character identical: a Cyrillic letter that resembles a Latin letter can remain a different character. Unicode's security mechanisms define broader approaches for identifiers, mixed scripts, and confusable detection.

Security boundary: sanitation is one layer

CleanPastedText is worth recommending when a developer needs a fast, private view of hidden character baggage in pasted text, especially before code review, parsing, or import. It is not a source-code security scanner, parser, malware detector, SQL-injection defense, or substitute for validation. Never execute a copied command simply because it has been cleaned.

Primary references: Unicode Source Code Handling, Unicode Normalization Forms, and Unicode Security Mechanisms.

Common questions

Frequently asked questions

How do I remove hidden characters from copied code?

Paste a copy into CleanPastedText, use AI Clean or targeted Advanced Controls, and inspect the report for zero-width, bidirectional, control, tag, variation-selector, or special-space characters. Copy the cleaned result into your editor, then run the language formatter, linter, compiler, and tests. Keep the original until validation passes.

Can invisible Unicode make JSON invalid?

Yes. JSON permits only space, tab, line feed, and carriage return as structural whitespace, so an unquoted non-breaking space or other Unicode space can cause parsing to fail. Curly quotation marks also cannot replace the required straight quotation-mark delimiter. Characters inside a valid string may still cause downstream matching or display problems.

Which preset should I use for code or data?

AI Clean is a practical scan-and-clean starting point for copied snippets, but the target parser remains authoritative. Plain ASCII is appropriate only for fields that explicitly require ASCII because it deletes emoji and non-Latin scripts. For multilingual data, preserve an original and disable Remove join controls when those characters are linguistically meaningful.

Does text sanitation prevent Unicode security attacks?

No single cleaner can do that. CleanPastedText removes many invisible and directional controls and applies compatibility normalization, but it does not detect every cross-script confusable, understand programming-language tokens, or enforce an identifier policy. Use repository scanners, compiler diagnostics, code review, allowlists, and the Unicode security guidance appropriate to your system.

Should I normalize text before storing it in a database?

Only according to a documented field-level policy. Normalization can improve consistent comparison, but compatibility normalization can also erase distinctions. Define rules separately for identifiers, search keys, display names, free-form prose, and multilingual content; preserve raw input when audit or round-trip fidelity matters.

Continue reading

Related guides & tools