Skip to content

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

Fix the "JSONDecodeError: Expecting ',' delimiter" in Python by identifying and correcting syntax issues like missing commas, trailing commas, or improperly escaped quotes in JSON arrays/objects.

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

invalid_json_string = '{ "employee":{ "name":"Alice \"developer\"" } }'

try:
    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
    
    valid_json_string = '{ "employee":{ "name":"Alice \\"developer\\"" } }'
    
    data = json.loads(valid_json_string)
    print(data)
  • 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
    
    valid_json_raw_string = r'{ "employee":{ "name":"Alice \"developer\"" } }'
    
    data = json.loads(valid_json_raw_string)
    print(data)

    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

invalid_array_string = '["name": "Alice", "age": 30]'

try:
    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

valid_array_string = '["name", "Alice", "age", 30]'

data = json.loads(valid_array_string)
print(data)

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

invalid_object_string = '{"name": "Alice", "age": 30 "city": "London"}'

try:
    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

valid_object_string = '{"name": "Alice", "age": 30, "city": "London"}'

data = json.loads(valid_object_string)
print(data)

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

invalid_trailing_array = '[10, 20, 30,]'
invalid_trailing_object = '{"name": "Alice", "city": "London",}'

try:
    data = json.loads(invalid_trailing_array)
except json.JSONDecodeError as e:
    print(f"Array Error: {e}")

try:
    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

valid_array = '[10, 20, 30]'
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

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

json_string = json.dumps(my_data, indent=2) # indent is optional for readability

print(json_string)

parsed_back = json.loads(json_string)
print("Parsed back:", parsed_back)

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.

Navigation