Skip to main content

How to Resolve Python Error "JSONDecodeError: Expecting ',' delimiter"

When parsing JSON data in Python using json.loads() or json.load(), the json.decoder.JSONDecodeError: Expecting ',' delimiter: line X column Y (char Z) indicates a syntax error within your JSON structure. Specifically, the parser encountered a position where it expected a comma (,) to separate elements (in an array) or key-value pairs (in an object), but found a different character instead.

This guide explains the common JSON syntax rules that lead to this error and provides clear solutions.

Understanding the Error: JSON Syntax Rules

JSON (JavaScript Object Notation) has strict syntax rules for structuring data:

  • Objects ({}): Collections of key-value pairs.
    • Keys must be strings enclosed in double quotes (").
    • Keys and values are separated by a colon (:).
    • Key-value pairs are separated from each other by a comma (,).
    • No trailing comma after the last pair.
    • Example: {"name": "Alice", "age": 30}
  • Arrays ([]): Ordered lists of values.
    • Values are separated from each other by a comma (,).
    • No trailing comma after the last value.
    • Example: ["apple", "banana", 123, true]
  • String Values: Must be enclosed in double quotes ("). Special characters inside strings (like double quotes themselves, backslashes) must be escaped with a backslash (\).
  • Other Values: Numbers (integers, floats), booleans (true, false - lowercase), null.

The "Expecting ',' delimiter" error means the parser reached a point (indicated by the line/column/character numbers) where, based on the structure it had parsed so far, it expected a comma to separate the next item/pair, but it encountered something else (like a closing brace }, closing bracket ], a colon :, a quote ", or the end of the input).

Common Cause 1: Incorrectly Escaped Quotes within String Values

If a string value within your JSON itself contains double quotes, these internal quotes must be properly escaped with a backslash (\). Forgetting to escape them, or escaping incorrectly, can make the parser think the string ended prematurely, leading it to expect a comma where one shouldn't be.

Error Scenario

import json

# Incorrect: The inner "developer" quotes are not escaped correctly for Python string literals
# Python interprets \" as just ", breaking the JSON structure internally
invalid_json_string = '{ "employee":{ "name":"Alice \"developer\"" } }'
# The parser sees '{"employee":{ "name":"Alice "' then 'developer' (unexpected token)

try:
# ⛔️ json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 31 (char 30)
# Error occurs after "Alice " because the next " is seen as closing the string value
data = json.loads(invalid_json_string)
print(data)
except json.JSONDecodeError as e:
print(e)

Solution 1: Properly Escape Internal Quotes (\\" or Raw Strings)

  • Using Double Backslash (\\"): In a standard Python string literal, a single backslash is an escape character for Python. To get a literal backslash into the string that JSON needs for escaping, you need to escape the backslash itself in Python using \\.

    import json

    # ✅ Correct: Escape inner quotes with double backslash \\"
    valid_json_string = '{ "employee":{ "name":"Alice \\"developer\\"" } }'

    data = json.loads(valid_json_string)
    print(data)
    # Output: {'employee': {'name': 'Alice "developer"'}}
  • Using Raw Strings (r"..."): If you prefix the string literal with r, Python treats backslashes more literally. In this case, a single backslash \" within the raw string is passed correctly to the JSON parser.

    import json

    # ✅ Correct: Use a raw string and single backslash escape \"
    valid_json_raw_string = r'{ "employee":{ "name":"Alice \"developer\"" } }'

    data = json.loads(valid_json_raw_string)
    print(data)
    # Output: {'employee': {'name': 'Alice "developer"'}}

    Raw strings are often easier for complex strings containing backslashes intended for the final data (like Windows paths or JSON escapes).

Common Cause 2: Incorrect Syntax in JSON Arrays (Using : instead of ,)

Trying to use key-value syntax (with colons :) inside a JSON array ([]) will cause this error because arrays expect comma-separated values.

Error Scenario

import json

# Incorrect: Using ':' like in an object, but inside array brackets '[]'
invalid_array_string = '["name": "Alice", "age": 30]'

try:
# ⛔️ json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 8 (char 7)
# Error occurs after "name" because it expects a comma or ']', not ':'
data = json.loads(invalid_array_string)
print(data)
except json.JSONDecodeError as e:
print(e)

Solution 2: Use Commas to Separate Array Elements

If you intend to create a JSON array, ensure elements are separated only by commas.

import json

# ✅ Correct: Array elements separated by commas
valid_array_string = '["name", "Alice", "age", 30]'

data = json.loads(valid_array_string)
print(data)
# Output: ['name', 'Alice', 'age', 30]

If you intended an object, use curly braces {} instead (see next section).

Common Cause 3: Incorrect Syntax in JSON Objects (e.g., Missing Commas)

Forgetting the comma between key-value pairs in a JSON object ({}) is a direct cause.

Error Scenario

import json

# Incorrect: Missing comma between "age": 30 and "city": "London"
invalid_object_string = '{"name": "Alice", "age": 30 "city": "London"}'

try:
# ⛔️ json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 29 (char 28)
# Error occurs after 30 because it expects a comma or '}', not '"city"'
data = json.loads(invalid_object_string)
print(data)
except json.JSONDecodeError as e:
print(e)

Solution 3: Use Commas Between Key-Value Pairs

Ensure every key-value pair in an object (except the last one) is followed by a comma.

import json

# ✅ Correct: Comma separates the key-value pairs
valid_object_string = '{"name": "Alice", "age": 30, "city": "London"}'

data = json.loads(valid_object_string)
print(data)
# Output: {'name': 'Alice', 'age': 30, 'city': 'London'}

Common Cause 4: Trailing Commas

JSON standard does not allow trailing commas after the last element in an array or the last key-value pair in an object.

Error Scenario

import json

# Incorrect: Trailing comma after 30
invalid_trailing_array = '[10, 20, 30,]'
# Incorrect: Trailing comma after "London"
invalid_trailing_object = '{"name": "Alice", "city": "London",}'

try:
# ⛔️ json.decoder.JSONDecodeError: Expecting value: line 1 column 13 (char 12)
data = json.loads(invalid_trailing_array)
except json.JSONDecodeError as e:
print(f"Array Error: {e}")

try:
# ⛔️ json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 37 (char 36)
data = json.loads(invalid_trailing_object)
except json.JSONDecodeError as e:
print(f"Object Error: {e}") # Error message might vary slightly

Solution 4: Remove Trailing Commas

Delete any commas appearing after the final element or key-value pair.

import json

# ✅ Correct: No trailing comma
valid_array = '[10, 20, 30]'
# ✅ Correct: No trailing comma
valid_object = '{"name": "Alice", "city": "London"}'

data_array = json.loads(valid_array)
data_object = json.loads(valid_object)

print(data_array) # Output: [10, 20, 30]
print(data_object) # Output: {'name': 'Alice', 'city': 'London'}

Best Practice: Validate Your JSON

If you are unsure about your JSON syntax, use an online JSON validator or a linter tool integrated into your editor. Paste your JSON string, and it will highlight syntax errors, including misplaced or missing commas and quoting issues.

Best Practice: Avoid Manual JSON String Construction

Manually building complex JSON strings in Python using string concatenation or formatting is error-prone. It's much safer and easier to build the equivalent Python data structure (list or dictionary) and then convert it to a valid JSON string using json.dumps(). This method automatically handles quoting and escaping correctly.

import json

# ✅ Build Python dictionary first
my_data = {
"employee": {
"name": 'Alice "developer"', # Use single quotes in Python for the value containing double quotes
"id": 101
},
"projects": ["Alpha", "Beta"]
}

# ✅ Convert Python object to JSON string reliably
json_string = json.dumps(my_data, indent=2) # indent is optional for readability

print(json_string)
# Output:
# {
# "employee": {
# "name": "Alice \"developer\"",
# "id": 101
# },
# "projects": [
# "Alpha",
# "Beta"
# ]
# }

# You can now parse this back without errors:
parsed_back = json.loads(json_string)
print("Parsed back:", parsed_back)
# Output: Parsed back: {'employee': {'name': 'Alice "developer"', 'id': 101}, 'projects': ['Alpha', 'Beta']}

Conclusion

The JSONDecodeError: Expecting ',' delimiter indicates a syntax problem in your JSON string where a comma separator was expected but not found. Common causes include:

  • Improperly escaped double quotes (") within string values. (Fix: Use \\" or raw strings r'...').
  • Using colons (:) instead of commas (,) to separate elements in a JSON array ([]). (Fix: Use commas).
  • Missing commas (,) between key-value pairs in a JSON object ({}). (Fix: Add commas).
  • Having trailing commas (,) after the last element/pair in an array or object. (Fix: Remove trailing commas).

Validate your JSON structure carefully, paying close attention to commas and quoting. Prefer using json.dumps() to generate JSON strings from Python objects rather than constructing them manually.