Why ChatGPT Breaks JSON When Copying (And How to Fix It)

If you have ever copied a perfectly formatted JSON payload from ChatGPT only to encounter SyntaxError: Unexpected token, the problem is rarely your logic. The issue is almost always invisible Unicode characters injected during copy-paste.

The Real Cause of "Unexpected token"

JSON is strict. According to the specification, only four whitespace characters are allowed outside strings:

  • Space (U+0020)
  • Horizontal Tab (U+0009)
  • Line Feed (U+000A)
  • Carriage Return (U+000D)

Anything else — even if it looks like a space — will cause JSON.parse() to fail immediately.

Common Hidden Characters That Break JSON

1. Zero Width Space (U+200B)

An invisible formatting character inserted by browsers to manage line wrapping. It has no visual width but is illegal in JSON.

2. Non-Breaking Space (U+00A0)

Looks identical to a normal space but is treated as a different Unicode character. JSON does not allow it.

3. Byte Order Mark (U+FEFF)

Used in some encoding contexts but invalid inside JSON strings or structural content.

Example of a Broken JSON Payload

{
  "server_config": {
    "host": "127.0.0.1",
    "port": 8080
  }
}

Programmatic Fix

function sanitizeJSONString(str) {
  return str
    .replace(/[\u200B-\u200D\uFEFF]/g, '')
    .replace(/\u00A0/g, ' ');
}

const clean = sanitizeJSONString(raw);
const parsed = JSON.parse(clean);

Faster Fix: Use a Dedicated Tool

All tools run 100% client-side. No uploads. No data storage.

Conclusion

AI-generated JSON is powerful but often contaminated with invisible formatting characters. Understanding U+200B, U+00A0, and U+FEFF prevents hours of debugging and ensures reliable parsing.

Sometimes the issue is caused specifically by a non-breaking space — see the difference between NBSP and regular space.

Related Guides