How do you choose between HTML, JSON, and C-style escapes?

A quote inside your words can accidentally end a JSON string. Escaping keeps it in your text, and each destination has its own spelling rules.

Why do quotes inside your text need escaping?

Escaping rewrites punctuation so the next app reads it as part of your text. It's like writing the same note for readers who use different punctuation rules: a quote inside your words needs a different spelling from a quote that ends a JSON string.

Original words: Say "hi". Adding outer quotes without escaping gives "Say "hi"", which ends the JSON string early. Escaping gives "Say \"hi\"", a valid string.

Keep the quote inside your words

  1. Start with your words

    The quote marks around hi are part of what you want to say.

  2. An inner quote can end the string

    Adding outer quotes isn't enough: JSON reads the next unescaped quote as the end of the value.

  3. A backslash changes how JSON reads it

    \" keeps that quote inside the words. Your text still says the same thing.

Syntax illustration: the original words, a broken JSON string, and its escaped version. How we reviewed

Which escapes fit HTML text, JSON strings, or code?

Choose by where your text will go. Here are the same words, Say "hi" <b>, written for three destinations; the pink characters are the escape spellings.

Original: Say "hi" <b>. HTML: Say &quot;hi&quot; &lt;b&gt;. JSON: "Say \"hi\" <b>". C-style: Say \"hi\" <b>. Each complete output appears under its destination.

One sentence, three escaped versions

  1. HTML entities: text between tags

    The browser reads &lt;b&gt; as visible text. For attributes or scripts, use your framework's escaping for that context.

  2. JSON string: one complete value

    The first and last quotes wrap the value. Quotes inside your words get a backslash; <b> stays as text.

  3. C-style escapes: string contents for code

    Use for the contents of a C or C++ string literal; add the surrounding quotes in your code. FluffyBench handles common escapes, not every language's string syntax.

Rendered for illustration with FluffyBench's escape operations. Displayed outputs contain no added line breaks.

Doing it in FluffyBench's Text escaper

In FluffyBench's Text escaper, From describes what you have and To describes what you need. Choose the destination above; the JSON path below shows how to take the result with you.

  1. From is Text; To opens the output choices.
    From is Text; To opens the output choices.
    Step 1. Set From to Text, then open To to choose an escape spelling.
    1. From: Text starts with your original words.
    2. Open To for the destination choices.
  2. The open To menu includes HTML entities, JSON string, and C-style escapes.
    The open To menu includes HTML entities, JSON string, and C-style escapes.
    Step 2. Choose HTML entities, JSON string, or C-style escapes to match the destination. This walkthrough continues with JSON string.
    1. HTML entities fits text between HTML tags.
    2. Choose JSON string for one complete JSON value.
    3. C-style escapes returns string contents for code.
  3. Say "hi" <b> in Input, with the phone Result tab.
    Say "hi" <b> in Input, with the phone Result tab.
    Step 3. Paste the source text into Input. Short text updates automatically; on a phone, open Result.
    1. Paste your original text in Input.
    2. On a phone, open Result.
  4. The JSON string result for Say "hi" <b>, with Copy and Download beside it.
    The JSON string result for Say "hi" <b>, with Copy and Download beside it.
    Step 4. Check the JSON string, including its outer quotes. Copy takes the complete result to your clipboard; Download saves a text file.
    1. The JSON string includes both outer quotes.
    2. Copy the complete result.
    3. Download the result as a text file.

Working with a whole JSON document? FluffyBench's JSON Formatter checks its syntax and formats it for you.

Search FluffyBench