Python runtime, API, data parsing python json api parsing

json.JSONDecodeError: Expecting property name enclosed in double quotes

Encountering json.JSONDecodeError: Expecting property name enclosed in double quotes means your JSON input is malformed, often due to incorrect quotation marks around property names; this guide explains how to fix it.

What This Error Means

This specific JSONDecodeError from Python's json module indicates that the parser encountered a JSON object structure ({...}) but found an invalid character or sequence where it strictly expected a property name enclosed in double quotes. JSON (JavaScript Object Notation) is a lightweight data-interchange format, but it has a very strict syntax defined by RFC 8259. One of its fundamental rules is that all property names (keys) within an object must be strings enclosed in double quotes. Single quotes (') or unquoted names are not permitted by the JSON specification.

When you see Expecting property name enclosed in double quotes, it's a clear signal that the data you're attempting to parse violates this core JSON rule. The Python json parser is explicitly telling you, "I'm looking for something like \"key\": but I found something else."

Why It Happens

The reason this error occurs boils down to the strictness of the JSON specification versus the leniency of other data formats or human-generated data. While JSON was inspired by JavaScript object literal syntax, it is significantly stricter.

In JavaScript, you can often define objects using single quotes for keys (e.g., {'name': 'Asha'}) or even unquoted keys if they are valid identifiers (e.g., {name: 'Asha'}). Many developers, especially those newer to strict data parsing, might inadvertently generate or expect such formats to be valid JSON. Python's json module, however, adheres to the official JSON standard without compromise.

When json.loads() or json.load() attempts to process a string or file:
1. It encounters an opening curly brace {, signifying the start of a JSON object.
2. It then expects the very next significant token to be a double quote " followed by a valid string (the property name), then a colon :, and then the property's value.
3. If instead it finds a single quote ', an unquoted word, or any other character that doesn't fit the " expectation at that point, it immediately raises json.JSONDecodeError: Expecting property name enclosed in double quotes.

In my experience, this is one of the most common JSONDecodeError types, frequently arising from human error when manually constructing JSON, or from systems that generate "JavaScript-like" objects rather than strictly conforming JSON.

Common Causes

This error almost always points to malformed JSON input. Here are the most frequent scenarios:

  • Single Quotes for Property Names: This is by far the leading cause. Developers coming from JavaScript or manually typing JSON often default to single quotes for string literals.
    json {'user_id': 1, 'username': 'alice'} // INCORRECT: single quotes for keys
    The json module expects {"user_id": 1, "username": "alice"}.

  • Unquoted Property Names: Another common issue, particularly if data is generated dynamically or through string concatenation without proper JSON serialization.
    json {user_id: 1, username: 'bob'} // INCORRECT: unquoted keys
    Again, {"user_id": 1, "username": "bob"} is the correct form.

  • Mixed Quotation Styles: Sometimes the JSON might appear mostly correct, but a single property name uses the wrong type of quote, leading to the same error. This can be harder to spot in large JSON blobs.

  • Data from Non-Strict Parsers/Tools: If you're consuming data from an external API, a database field, or a logging system, that system might have been configured to output a more lenient "JavaScript-like" format rather than strict JSON. I've seen this in production when a third-party service had an internal bug in their JSON serializer, leading to single quotes in their otherwise valid payload.

  • Manual Construction Errors: When developers manually concatenate strings to form JSON, it's easy to make mistakes with quotation marks. Always prefer using json.dumps() to serialize Python dictionaries into JSON strings.

Step-by-Step Fix

Rectifying this error involves identifying the source of the malformed JSON and correcting it. Here's a systematic approach:

  1. Identify the Source of the JSON:
    First, pinpoint where the problematic JSON string is originating. Is it:

    • An API response (internal or external)?
    • A configuration file (e.g., .json file, .env variable)?
    • Data fetched from a database?
    • User input?
    • A cached value?
    • Hardcoded within your Python script?
  2. Inspect the Raw JSON String:
    This is the most critical step. Do not trust what you think the JSON looks like; verify its actual content.

    • In Python: Print the raw string using print() or logging.info(). If it's coming from a variable json_data_str, use print(f"Raw JSON string: {json_data_str}") before attempting json.loads(). For even more clarity, especially with escape characters, print(repr(json_data_str)) can be useful.
    • From an API: Use curl or Postman to fetch the raw response directly, bypassing your application's parsing logic.
      bash curl -X GET "https://api.example.com/data"
    • From a File: Open the file in a text editor.

    Once you have the raw string, visually inspect it for property names that are enclosed in single quotes or that lack quotes entirely.

  3. Correct the JSON at its Source (Preferred Method):
    The most robust and recommended solution is to fix the source that generates the malformed JSON.

    • If you control the source (your API, your config, your database insert): Modify the code or process to ensure it outputs strictly compliant JSON. Use json.dumps() in Python, JSON.stringify() in JavaScript, or equivalent serialization functions in other languages. Avoid manual string concatenation for JSON.
      ```python
      import json

      BAD: Manual string building

      my_data_bad = "{'name': 'Asha', 'title': 'Architect'}"

      GOOD: Using Python dictionary and json.dumps()

      my_data_dict = {"name": "Asha", "title": "Architect"}
      my_data_good = json.dumps(my_data_dict)
      print(my_data_good) # Output: {"name": "Asha", "title": "Architect"}
      ```
      * If it's an external API: Report the bug to the API provider. Explain that their API is returning non-compliant JSON, specifically regarding property name quotation. Provide them with an example of the problematic response.

  4. Pre-process the JSON String (Last Resort with Extreme Caution):
    If you absolutely cannot fix the source and the data is critical, you might attempt to "sanitize" the string before parsing. This approach is fragile and can lead to new errors if not handled meticulously. It's often necessary when dealing with legacy systems or non-standard data producers.

    • Dangerous replace(): A common but highly risky approach is to globally replace single quotes with double quotes: fixed_str = original_str.replace("'", '"').
      WARNING: This is extremely dangerous. If any value within your JSON string legitimately contains a single quote (e.g., {"description": "It's a wonderful day."}), this replacement will break your JSON into {"description": "It"s a wonderful day."}, leading to a new JSONDecodeError: Expecting value or similar. Avoid this unless you have absolute certainty about the data's content.

    • Targeted Regex (Better, but still complex): A more robust (but still not foolproof) approach involves using regular expressions to specifically target single-quoted keys. This requires careful regex construction.
      ```python
      import re
      import json

      def fix_single_quoted_keys(json_string):
      # This regex attempts to find single-quoted keys and convert them to double quotes.
      # It targets patterns like: { 'key':, , 'key':, and ensures it's a key before a colon.
      # It's not perfect for all edge cases but covers the most common malformation.
      return re.sub(r"([{,]\s)'([^']+?)'(\s:\s*)", r'\1"\2"\3', json_string)

      malformed_json = "{'product_id': 123, 'name': 'Fancy Gadget'}"
      try:
      fixed_json = fix_single_quoted_keys(malformed_json)
      print(f"Fixed string: {fixed_json}")
      data = json.loads(fixed_json)
      print(f"Successfully parsed: {data}")
      except json.JSONDecodeError as e:
      print(f"Error after attempted fix: {e}")

      Example where the simple regex might not cover:

      If the original JSON is { 'user-data': { 'name': 'O\'Malley' } }

      The regex aims to fix the key, but the value's single quote is tricky.

      This highlights why fixing the source is always better.

      ```

  5. Validate JSON:
    Once you have a supposedly fixed JSON string, validate it before integrating it.

    • Use an online JSON validator.
    • Use Python's built-in json.tool from the command line:
      ```bash
      echo "{'name': 'Asha'}" | python -m json.tool
      # Expected Output: json.tool: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

      echo '{"name": "Asha"}' | python -m json.tool

      Expected Output: (pretty-printed valid JSON)

      {

      "name": "Asha"

      }

      ```

Code Examples

Here are illustrative examples demonstrating the error and correct JSON parsing.

1. Incorrect JSON (Causing the Error)

import json

# Scenario 1: Single quotes used for property names
malformed_json_single_quotes = "{'user_id': 123, 'username': 'janedoe', 'status': 'active'}"
print(f"Attempting to parse: {malformed_json_single_quotes}")
try:
    data = json.loads(malformed_json_single_quotes)
    print(f"Parsed data: {data}")
except json.JSONDecodeError as e:
    print(f"Error caught: {e}")
# Expected output: Error caught: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

print("-" * 30)

# Scenario 2: Unquoted property names
malformed_json_unquoted_names = "{user_id: 456, 'username': 'johndoe', status: 'inactive'}"
print(f"Attempting to parse: {malformed_json_unquoted_names}")
try:
    data = json.loads(malformed_json_unquoted_names)
    print(f"Parsed data: {data}")
except json.JSONDecodeError as e:
    print(f"Error caught: {e}")
# Expected output: Error caught: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

2. Correct JSON (How it Should Look)

import json

# Correct JSON with double quotes for all property names
correct_json_str = '{"user_id": 789, "username": "alice", "status": "pending"}'
print(f"Attempting to parse: {correct_json_str}")
try:
    data = json.loads(correct_json_str)
    print(f"Successfully parsed: {data}")
except json.JSONDecodeError as e:
    print(f"Error caught: {e}")
# Expected output: Successfully parsed: {'user_id': 789, 'username': 'alice', 'status': 'pending'}

print("-" * 30)

# Using json.dumps() to ensure proper JSON serialization from a Python dictionary
python_dict = {
    "item_id": 101,
    "item_name": "Widget Alpha",
    "price": 29.99,
    "available": True
}
correct_json_output = json.dumps(python_dict, indent=4) # indent for readability
print("Correctly serialized JSON (using json.dumps):")
print(correct_json_output)
# Expected output: Valid, pretty-printed JSON

Environment-Specific Notes

The json.JSONDecodeError: Expecting property name enclosed in double quotes can manifest differently or be harder to diagnose depending on your deployment environment.

  • Cloud Functions/Lambdas (AWS Lambda, Google Cloud Functions, Azure Functions):
    When running serverless functions, the source of your JSON might be an API Gateway request body or the output of another function. Ensure that any input data is correctly formatted, and crucially, that your function's return value (which often needs to be JSON for API Gateways) is serialized using json.dumps(). Troubleshooting means checking cloud logs extensively. In my experience, if your Lambda returns invalid JSON, API Gateway might return a generic 500 error or a malformed response that's difficult to debug from the client side. Always check the raw function logs first.

  • Docker/Containers:
    Applications running inside Docker containers might receive malformed JSON from various sources:

    • Environment Variables: If you pass complex JSON structures as environment variables, ensure they are correctly escaped and double-quoted. docker run -e 'MY_CONFIG={"key":"value"}' vs. docker run -e "MY_CONFIG={'key':'value'}" (note the outer shell quoting and inner JSON quoting).
    • Volume-mounted Configuration Files: If your container reads JSON from a file mounted via a Docker volume, inspect the original file on the host and ensure it's valid.
    • Inter-service Communication: Services communicating over a network might pass malformed JSON if one service isn't serializing correctly. Use container logs or network traffic inspection tools (like tcpdump if necessary) to see the raw data exchanged.
  • Local Development:
    This is generally the easiest environment to debug. You have immediate access to stdout/stderr. Leverage print statements extensively to inspect the JSON string just before json.loads(). Use a debugger (like pdb or an IDE's debugger) to step through your code and examine variable values. The repr() function on the string (print(repr(my_json_string))) is particularly useful locally as it shows you the exact representation of the string, including any tricky escape characters or invisible characters.

  • API Integrations (Consuming External Services):
    When consuming data from an external API, the Content-Type: application/json header might be misleading. I've encountered situations where an API advertised JSON but returned a response body with single-quoted keys due to a backend bug. Always log the raw response body from the external API call before attempting to parse it. If the raw response is consistently malformed, you'll need to report it to the API provider.

Frequently Asked Questions

Q: Why does Python's json module complain but JavaScript seems to handle it fine?
A: Python's json module adheres strictly to the RFC 8259 JSON specification, which requires double quotes for property names. JavaScript, while JSON's progenitor, is more lenient. In JavaScript, you can use single quotes or even unquoted names (for valid identifiers) in object literals directly in your code. However, when JavaScript uses JSON.parse() (to process actual JSON strings) or JSON.stringify() (to generate JSON strings), it also enforces and expects double quotes, adhering to the JSON standard.

Q: Can I just globally replace all single quotes with double quotes?
A: No, this is highly discouraged and often leads to new JSONDecodeErrors or incorrect parsing. If a string value within your JSON legitimately contains a single quote (e.g., {"description": "It's great!"}), a global replacement would break the JSON structure, turning it into {"description": "It"s great!"}, which is invalid. Only use this as an absolute last resort if you are unequivocally certain that no string values contain single quotes, which is a rare and dangerous assumption to make.

Q: I'm getting this error from an API response. How do I fix it?
A: You cannot directly fix the upstream API's output. Your primary options are:
1. Contact the API provider: Report the issue to them, explaining that their API returns non-compliant JSON regarding property name quotation. This is the most robust long-term solution.
2. Implement careful pre-processing (last resort): If the data is critical and you cannot wait for the API provider to fix it, you might have to implement a careful pre-processing step to sanitize the string on your end (e.g., using a targeted regex as shown in the "Step-by-Step Fix" section). However, be aware of the fragility of this approach.

Q: Does this error apply to JSON arrays as well?
A: This specific error, Expecting property name enclosed in double quotes, primarily applies to JSON objects (Python dictionaries) where keys are expected. An array [{"key": "value"}, {"key2": "value2"}] itself doesn't have keys, but if the array contains objects, and those objects have single-quoted or unquoted keys (e.g., [{'key': 'value'}]), then parsing that inner object will trigger this exact error. A simple array of strings ['item1', 'item2'] would likely cause a different JSONDecodeError (e.g., Expecting value) if its string elements were not double-quoted.