DEV Community

Alex Sanghez
Alex Sanghez

Posted on

Why single quotes break your JSON (and how to fix it without wrecking your data)

You copied some JSON, the parser exploded, and the error says something like:

Unexpected token ' in JSON at position 2

The culprit is almost always single quotes. Here's exactly why it happens
and how to fix it safely.

Why JSON is this strict about quotes

JSON was designed to be boring on purpose. Every parser, in every language,
agrees on what a string is because the format only allows double quotes.
No exceptions.

JavaScript makes this feel confusing. Single quotes are perfectly valid
in a JS object literal — but a JS object literal is not JSON. The moment
that payload hits JSON.parse(), the JSON rules win.

The three patterns people hit most

Invalid: all single quotes

{
  'name': 'StructKit',
  'mode': 'validator'
}
Enter fullscreen mode Exit fullscreen mode

Invalid: even just one

{
  "name": 'StructKit',
  "mode": "validator"
}
Enter fullscreen mode Exit fullscreen mode

One single-quoted value is enough to blow up the whole payload.

Valid: double quotes throughout

{
  "name": "StructKit",
  "mode": "validator"
}
Enter fullscreen mode Exit fullscreen mode

What the error looks like in each runtime

Runtime Error message
JSON.parse Unexpected token ' in JSON at position 2
json.loads Expecting property name enclosed in double quotes
API client Invalid JSON near key or opening character

The Python trap

Python is the most common case. Dict literals use single quotes naturally,
so it's easy to forget that json.loads parses strict JSON, not Python syntax.

# ❌ This fails
payload = "{'name': 'StructKit'}"
json.loads(payload)  # JSONDecodeError

# ✅ This works
data = {"name": "StructKit"}
payload = json.dumps(data)
json.loads(payload)
Enter fullscreen mode Exit fullscreen mode

How to fix it without breaking your data

A blind find-and-replace from ' to " is how you turn one error into two.
Apostrophes inside real text — like "user's name" — are valid and should
stay as they are. Replace only the quotes acting as JSON delimiters.

This is valid JSON:

{
  "message": "It's valid JSON",
  "owner": "Alex's tool"
}
Enter fullscreen mode Exit fullscreen mode

If your payload is still failing after fixing the quotes, a JSON validator
that shows you the exact line and column helps a lot. I built one at
structkit.dev alongside a few reference guides
for exactly these kinds of errors.

Top comments (0)