Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool

Why Your JSON Formatter Shows a Red Error Message: Troubleshooting Guide

If your JSON formatter suddenly turns part of the document red, the usual reason is simple: the input is not valid JSON yet. In practice, this often happens when someone pastes JavaScript, HTML, or a <script> snippet into a formatter that only accepts strict JSON.

The fastest way to diagnose it is to ask one question first: are you formatting actual JSON, or are you formatting data that still contains JavaScript syntax or unescaped markup? Once you separate those cases, the red error is usually easy to fix.

Quick answer

  • JSON requires double-quoted keys and strings.
  • JSON does not allow comments, trailing commas, functions, or unquoted property names.
  • Script or HTML content must be stored as a JSON string, with quotes and line breaks escaped.
  • If you embed JSON inside an HTML <script type="application/json"> block, a literal </script> inside the data can break the page unless it is escaped.

What the red error usually means

A formatter highlights text in red when its parser can no longer read the document as valid JSON. The exact wording varies by tool and browser engine, but the rules are stable: JSON supports objects, arrays, strings, numbers, true, false, and null. Property names and string values must use double quotes, and control characters inside strings must be escaped.

Common red-error triggers

  • Single quotes such as 'name' instead of "name"
  • Trailing commas before ] or }
  • Comments like // note or /* note */
  • Functions, undefined, NaN, or other JavaScript-only values
  • Raw line breaks, tabs, or unescaped quotes inside a string
  • Copy-pasted HTML or script code that is not wrapped as a JSON string

Why script content often shows in red

Searchers who describe this as "script inside JSON shows in red" are usually hitting one of three cases below.

1. You pasted JavaScript object syntax, not JSON

{
  user: 'Ada',
  enabled: true,
  run: () => console.log('hi')
}

This is valid JavaScript syntax, but it is not valid JSON. The unquoted key, single quotes, and function value will all make a JSON formatter complain.

2. The script or HTML is not encoded as a JSON string

{
  "snippet": "<script>
  alert("hi")
</script>"
}

The inner quotes around "hi" and the raw line breaks must be escaped. Otherwise the JSON string ends early and the rest of the text turns red.

3. You embedded JSON inside an HTML script block

<script type="application/json">
{"snippet":"</script>"}
</script>

In this context, the HTML parser sees the literal </script> and closes the element. The JSON may be valid by itself, but the page breaks because the surrounding HTML parser stops reading the data block where you did not expect it to.

How to fix each case

Convert JavaScript-looking data into real JSON

{
  "user": "Ada",
  "enabled": true,
  "run": "console.log('hi')"
}

Every key uses double quotes. String values use double quotes. The function is no longer treated as code.

Store script text as a properly escaped JSON string

{
  "snippet": "<script>\n  alert(\"hi\")\n</script>"
}

The content is just text now. The quotes are escaped as \", and line breaks are escaped as \n.

If the JSON will live inside an HTML script tag, escape the tag text too

<script type="application/json">
{"snippet":"\u003Cscript>alert(\"hi\")\u003C/script>"}
</script>

Escaping the less-than character as \u003C is the safest option when serializing JSON into HTML, because it prevents accidental <script> and </script>sequences in the raw page source.

Use the error text as a clue, not a verdict

The line the formatter highlights is often where the parser finally gave up, not where the mistake started. If the red marker appears on one line, inspect the line above it too.

Unexpected token <

You probably pasted HTML, an error page, or raw <script> markup where JSON was expected.

Unexpected token '

Single quotes are common in JavaScript objects, but JSON requires double quotes.

Unexpected end of JSON input

A quote, brace, or bracket is missing, or the data was cut off during copy and paste.

Bad control character in string literal

A raw tab, newline, or other control character was pasted inside a string instead of being escaped.

The safest fix: let code escape it for you

If you are generating JSON from application code, do not hand-escape script snippets or HTML. Build a normal object and let a serializer produce valid JSON for you.

const payload = {
  snippet: '<script>alert("hi")</script>',
};

const json = JSON.stringify(payload);
const htmlSafeJson = json.replace(/</g, "\\u003C");

JSON.stringify() handles quotes, slashes, and control characters correctly. If you then place that JSON into HTML, replacing < with \u003C avoids script-tag parsing issues in the page source.

If the content is red but the JSON still formats

Not every red highlight means invalid JSON. Some editors flag words like <script> because they look dangerous in an HTML context. If the formatter successfully parses and pretty-prints the document, your JSON syntax is valid. The remaining question is context: where will that JSON be rendered, embedded, or executed?

  • If the formatter refuses to parse, you have a JSON syntax problem.
  • If it parses but the page still breaks, you probably have an HTML embedding problem.
  • If it parses and the page works, the red color may just be a highlighter or security warning.

Practical checklist

  • Make sure every key and string uses double quotes.
  • Remove comments, trailing commas, and functions.
  • Escape inner quotes and line breaks inside HTML or script snippets.
  • Check the line above the red marker for the real mistake.
  • If the JSON is embedded into HTML, escape < as \u003C.
  • When possible, generate JSON with a serializer instead of editing large blobs by hand.

In short, red text in a JSON formatter almost never means the word "script" itself is forbidden. It usually means the surrounding content is not strict JSON yet, or that valid JSON is being dropped into HTML without the extra escaping that HTML parsing requires.

Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool