DEV Community

AI JSONMedic
AI JSONMedic

Posted on • Originally published at aijsonmedic.com

How to Fix JSON Single Quotes: The Complete Guide

Single-quoted JSON breaks every parser — Python's json.loads, JavaScript's JSON.parse, and every API client on the planet. The fix is straightforward once you understand the rule.

This article was originally published at AI JSONMedic.


Why JSON Rejects Single Quotes

JSON is defined by RFC 8259, which requires all strings — both keys and values — to be wrapped in double quotes only. Single quotes are simply not part of the JSON specification.

This means the following is invalid JSON:

// INVALID
{'name': 'Alice', 'age': 30}
Enter fullscreen mode Exit fullscreen mode

The valid form:

// VALID
{"name": "Alice", "age": 30}
Enter fullscreen mode Exit fullscreen mode

There is no parser setting or "loose mode" that accepts single-quoted JSON.

What Causes the Single Quote Error

Python dictionary literals. When you call str() on a Python dict, Python prints it with single quotes. This is NOT valid JSON:

data = {"name": "Alice"}
print(str(data))
# Output: {'name': 'Alice'}  ← NOT valid JSON
Enter fullscreen mode Exit fullscreen mode

LLM and AI output. Language models sometimes generate Python-dict-style output instead of strict JSON.

Manual editing. Developers who write JSON by hand sometimes use single quotes from JavaScript or Python habit.

Fix in Python

Option 1 — Use json.dumps() instead of str()

import json

data = {"name": "Alice", "age": 30}
good = json.dumps(data)
print(good)
# Output: {"name": "Alice", "age": 30}
Enter fullscreen mode Exit fullscreen mode

Option 2 — ast.literal_eval for Python dict strings

If the string is specifically Python dict syntax, ast.literal_eval is the most reliable repair:

import ast, json

broken = "{'name': \"it's Alice\", 'city': 'London'}"
data = ast.literal_eval(broken)
valid_json = json.dumps(data)
Enter fullscreen mode Exit fullscreen mode

Option 3 — json-repair library for production

from json_repair import repair_json

broken = "{'name': 'Alice', 'scores': [95, 87, 91]}"
fixed = repair_json(broken)
# Output: {"name": "Alice", "scores": [95, 87, 91]}
Enter fullscreen mode Exit fullscreen mode

Fix in JavaScript

// Never build JSON with single quotes — use JSON.stringify()
const data = { name: "Alice", age: 30 };
const validJson = JSON.stringify(data);

// For basic strings without apostrophes in values:
const broken = "{'name': 'Alice', 'age': 30}";
const fixed = broken.replace(/'/g, '"');
const obj = JSON.parse(fixed);
Enter fullscreen mode Exit fullscreen mode

Online Fix

For a quick browser-based repair without writing any code, AI JSONMedic automatically detects and fixes single quotes, trailing commas, Python booleans (True/False/None), and other common JSON errors.

Summary

Scenario Fix
Python dict → JSON Use json.dumps()
Python dict string Use ast.literal_eval() then json.dumps()
LLM / production pipeline Use json-repair library
JavaScript Use JSON.stringify()
Quick one-off repair AI JSONMedic

Top comments (0)