json.decoder.JSONDecodeError: Invalid control character at

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# json.decoder.JSONDecodeError: Invalid control character at

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.

main.py
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)

string contains control character

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.

# Forgetting to close a quote in a JSON string

You might also get the error if you forget to close a quote of a property or a value in your JSON string.

main.py
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)

missing closing quote on json string

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.

# Set the strict argument to 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.

main.py
import json json_str = '{"name": "Bobby Hadz"}' # 👇️ set strict to False result = json.loads(json_str, strict=False) print(result) # 👉️ {'name': 'Bobby Hadz'}

set strict argument to false when calling json loads

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.

By default, strict is set to True, so no control characters are allowed in the JSON string.

# Making sure your JSON string is valid

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.

main.py
json_str = r'{"name": "Bobby Hadz"}'
Strings that are prefixed with r are called raw strings and treat special characters such as backslashes as literal characters.

# Setting strict to False when opening a File

You can also set the strict keyword argument to False when using the json.load() method to read from a JSON file.

main.py
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}

set strict to false when opening file

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.

# Removing or escaping control characters

An alternative way to solve the error is to remove the control characters from the JSON string.

main.py
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'}

remove or escape control characters

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.

main.py
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'}

escape control characters

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.

# Use a triple-quoted string to not have to escape quotes

You can use a triple-quoted string if your JSON string contains single quotes.

main.py
import json json_str = r"""{"name": "Bobby Hadz 'abc'"}""" result = json.loads(json_str, strict=False) print(result) # 👉️ {'name': "Bobby Hadz 'abc'"}

using triple quoted string to not have to escape quotes

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.