Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool

String Delimiter Issues in JSON: Single vs. Double Quotes

One of the most common errors when working with JSON is using the wrong type of quotes for strings. Unlike many programming languages that accept either single or double quotes, JSON has a strict requirement: strings must be enclosed in double quotes only. This article explains why this restriction exists, how it causes problems, and how to avoid or fix these issues.

The JSON Specification and Quotes

According to the official JSON specification, strings in JSON must be enclosed in double quotes. This is a non-negotiable rule that affects both keys and values:

Valid JSON:

{
  "name": "John Doe",
  "age": 30,
  "preferences": {
    "theme": "dark",
    "notifications": true
  }
}

Common Errors with String Delimiters

1. Using Single Quotes for Keys or Values

Developers familiar with JavaScript, Python, or other languages often mistakenly use single quotes in JSON.

Invalid JSON with single quotes:

{
  'name': 'John Doe',
  'age': 30
}

This will trigger a JSON parse error: Expected property name or }

2. Mixing Quote Types

Another common mistake is mixing quote types within the same JSON document.

Invalid JSON with mixed quotes:

{
  "name": 'John Doe',
  'age': 30
}

This will result in a syntax error since all strings must use double quotes consistently.

Why Only Double Quotes?

The JSON specification chose double quotes for several reasons:

  • Simplicity: Having one way to represent strings eliminates ambiguity
  • Compatibility: Double quotes are recognized across virtually all programming languages
  • Historical reasons: JSON was derived from JavaScript object notation, which supports double quotes for strings
  • Readability: Double quotes provide clear visual boundaries for string content

How to Fix Quote Delimiter Issues

1. Manual Replacement

The simplest approach is to replace all single quotes with double quotes. Most text editors support find-and-replace operations with regular expressions:

  • Find: '([^']*)'
  • Replace with: "$1"

This regular expression finds text between single quotes and replaces the single quotes with double quotes.

2. Using JSON Formatter Tools

JSON formatter tools like the one provided by Offline Tools can automatically detect and repair quote delimiter issues. Simply paste your JSON with incorrect quotes, and the tool will attempt to fix the formatting.

3. Escaping Quotes in Strings

When you need to include double quotes inside a string in valid JSON, they must be escaped with a backslash:

Correct escaping of quotes:

{
  "message": "He said, \"Hello world!\"",
  "code": "console.log(\"testing\");"
}

Language-Specific Considerations

JavaScript

JavaScript allows single quotes for strings in code, but not when parsing JSON:

// This works in JavaScript
const obj = {
  'name': 'John'
};

// But this fails
const jsonString = '{"name": "John"}'; // Valid JSON string
const jsonString2 = "{'name': 'John'}"; // Invalid JSON string

// This will work
JSON.parse(jsonString); 

// This will throw an error
JSON.parse(jsonString2);

Python

Python developers need to be especially careful, as Python's dictionaries commonly use single quotes:

# Python dictionary with single quotes
python_dict = {
    'name': 'John',
    'age': 30
}

# Converting to valid JSON requires double quotes
import json
json_string = json.dumps(python_dict)  # Results in {"name": "John", "age": 30}

Important Note:

When programmatically generating JSON, use your language's built-in JSON serialization functions rather than constructing JSON strings manually. This helps avoid quote delimiter issues and other syntax errors.

Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool