Single quotes in JSON

JSON defines exactly one string delimiter: the double quote. Single quotes are not an alternative form, they are not valid anywhere, and that applies to property names as well as to values.

How the message appears

The same fault, worded by different engines. V8 (Chrome, Node, Edge) changed its format around Chrome 109, so older and newer runtimes disagree about the same document.

  • SyntaxError: Unexpected token ''', "{'name': "... is not valid JSON
  • SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data

The fix

Replace every single quote around a key or string value with a double quote. true, numbers and null are never quoted at all.

Fails to parse
{
  'name': 'Ada',
  'active': true
}
Parses
{
  "name": "Ada",
  "active": true
}

The example travels in the URL fragment, which browsers never send to a server.

What causes it

Pasting a JavaScript or Python literal

Both languages treat ' and " as interchangeable, so a dict or object literal copied straight out of source is usually not valid JSON. Python adds two more traps in the same paste: True/False/None instead of true/false/null.

Smart quotes from a document or chat client

Word, Google Docs, Slack and Notion silently replace straight quotes with the typographic pair “ ”. They look almost identical in a proportional font and are not quotes as far as a parser is concerned.

Shell quoting

Wrapping a JSON payload in single quotes for curl is correct; letting those quotes end up inside the payload is not. If the JSON itself contains single quotes after the shell is done with it, the quoting nested one level too deep.

Worth knowing

  • A double quote inside a string value has to be escaped as \" once you switch. This is why a blind find-and-replace is risky and parsing is not.
  • Python users: json.dumps(obj) emits valid JSON; str(dict) and print(dict) do not.

Written by Vishnu Shankar.

Your JSON and text are processed entirely in your browser and are never sent anywhere. To understand how the site is used we record each page view (the page, the referrer and an approximate location from your IP address) and keep it for 90 days. Nothing is stored on your device unless you choose it. Accept to also store a device identifier here and record your browser and device characteristics, which is what lets a return visit be told from a new one. Decline and nothing is sent at all. We respect Do Not Track. See our privacy policy.