Last updated: Apr 8, 2024
Reading time·4 min
The Python "json.decoder.JSONDecodeError: Invalid control character at" is
caused when we try to parse a JSON string that contains control characters
without setting strict
to False
.
To solve the error, set the strict
keyword argument to False
in the call
to json.loads()
.
Here is an example of how the error occurs.
import json json_str = '{"name": "Bobby\n Hadz"}' # ⛔️ json.decoder.JSONDecodeError: Invalid control character at: line 1 column 16 (char 15) result = json.loads(json_str)
Notice that the JSON string in the example contains a control character - a
newline \n
character.
Control characters are non-printing characters, e.g. a newline character \n
or
a tab \t
.
You might also get the error if you forget to close a quote of a property or a value in your JSON string.
import json json_str = '{"name": "Bobby Hadz}' # ⛔️ json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 10 (char 9) result = json.loads(json_str)
Notice that we have a missing double quote at the end of the value.
Make sure that all quotes in your JSON string are closed correctly.
False
when calling json.loads()
To solve the error, make sure you don't have any missing closing quotes in your
JSON string and set the strict
keyword argument to False
.
import json json_str = '{"name": "Bobby Hadz"}' # 👇️ set strict to False result = json.loads(json_str, strict=False) print(result) # 👉️ {'name': 'Bobby Hadz'}
If strict
is set to False
, then
control characters are allowed inside strings.
Control characters are those with character codes in the 0-31
range, e.g.
\t
, \n
, \r
, \0
, etc.
strict
is set to True
, so no control characters are allowed in the JSON string.You have to make sure the JSON data you are trying to parse is valid.
The fastest way to validate and correct your JSON is to use a JSON validator.
Paste your payload into the form as the validator checks for errors and sometimes directly fixes them.
If you got the error when copy-pasting a JSON string in your Python code, prefix
the JSON string with r
to mark it as a
raw string.
json_str = r'{"name": "Bobby Hadz"}'
r
are called raw strings and treat special characters such as backslashes as literal characters.strict
to False
when opening a FileYou can also set the strict
keyword argument to False
when using the
json.load()
method to read from a JSON file.
import json file_name = 'example.json' with open(file_name, 'r', encoding='utf-8') as f: my_data = json.load(f, strict=False) print(my_data) # 👉️ {'name': 'Alice', 'age': 30}
The json.load() method is used to deserialize a file to a Python object, whereas the json.loads() method is used to deserialize a JSON string to a Python object.
An alternative way to solve the error is to remove the control characters from the JSON string.
import json json_str = '{"name": "Bobby\n Hadz"}' print(repr(json_str)) # 👉️ '{"name": "Bobby\n Hadz"}' json_str = json_str.replace('\n', '') print(json_str) # 👉️ {"name": "Bobby Hadz"} result = json.loads(json_str) print(result) # 👉️ {'name': 'Bobby Hadz'}
We used the repr() function to print the control characters in the string.
The JSON string contains a newline \n
character, so we removed it by replacing
it with an empty string.
The new string stores valid JSON without any control characters, so it can be parsed without any issues.
Alternatively, you can escape the control characters.
import json json_str = '{"name": "Bobby\n Hadz"}' print(repr(json_str)) # 👉️ '{"name": "Bobby\n Hadz"}' json_str = json_str.replace('\n', '\\n') print(json_str) # 👉️ {"name": "Bobby\n Hadz"} result = json.loads(json_str) print(result) # 👉️ {'name': 'Bobby\n Hadz'}
The code sample escapes the newline \n
character with a second backslash.
The second backslash makes it so the \n
character is treated as a literal
character and not a control character.
You can use a triple-quoted string if your JSON string contains single quotes.
import json json_str = r"""{"name": "Bobby Hadz 'abc'"}""" result = json.loads(json_str, strict=False) print(result) # 👉️ {'name': "Bobby Hadz 'abc'"}
The string in the example contains single and double quotes.
To not have to escape the single quotes in the string, we used a triple-quoted string.
You can learn more about the related topics by checking out the following tutorials: