Menu

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

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

DEV CommunityΒ·Alex SanghezΒ·7 months ago
#nLRskx3b
#javascript#json#python#quotes#payload
Reading 0:00
15s threshold

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"…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.