Of course! This is a very common error.
The problem is that the JSON format strictly requires double quotes (") for keys and string values, but your string z uses single quotes (').
While {'created': 1749695254} is a valid Python dictionary, it is not a valid JSON string.
The Fix
Simply change the single quotes around the key 'created' to double quotes.
import json
# The key 'created' must be in double quotes to be valid JSON.
# We use single quotes for the overall Python string to avoid conflicts.
z = '{"created": 1749695254}'
# Now this will work without error
y = json.loads(z)
# Let's print the result to verify
print(y)
print(f"The type of y is: {type(y)}")
Output:
{'created': 1749695254}
The type of y is: <class 'dict'>
Why this happens: JSON vs. Python Dictionaries
It's easy to get them confused because they look so similar.
| Feature | JSON Object | Python Dictionary |
|---|
| Keys | Must be strings in double quotes ("). | Can be many immutable types, but string keys can use single (') or double (") quotes. |
| String Values | Must be in double quotes ("). | Can be in single (') or double (") quotes. |
true/false | true, false (lowercase) | True, False (capitalized) |
null | null | None |
Your original string "{'created': 1749695254}" was trying to be parsed by JSON rules but was written with Python's more flexible dictionary syntax, causing the JSONDecodeError.
Alternative Solution: If you can't edit the string
If you are receiving a string like this from an API or source you can't control, you can sometimes use Python's ast (Abstract Syntax Tree) module, which is designed to safely evaluate string representations of Python literals.
import ast
z = "{'created': 1749695254}"
# ast.literal_eval safely parses a string containing a Python literal (like a dict)
y = ast.literal_eval(z)
print(y)
print(f"The type of y is: {type(y)}")
This is often a better choice than a simple string.replace("'", '"'), which can be brittle and break if the string values themselves contain apostrophes.