The built-in JSON module can be used as a validator:

import json

def parse(text):
    try:
        return json.loads(text)
    except ValueError as e:
        print('invalid json: %s' % e)
        return None # or: raise

You can make it work with files by using:

with open(filename) as f:
    return json.load(f)

instead of json.loads and you can include the filename as well in the error message.

On Python 3.3.5, for {test: "foo"}, I get:

invalid json: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

and on 2.7.6:

invalid json: Expecting property name: line 1 column 2 (char 1)

This is because the correct json is {"test": "foo"}.

When handling the invalid files, it is best to not process them any further. You can build a skipped.txt file listing the files with the error, so they can be checked and fixed by hand.

If possible, you should check the site/program that generated the invalid json files, fix that and then re-generate the json file. Otherwise, you are going to keep having new files that are invalid JSON.

Failing that, you will need to write a custom json parser that fixes common errors. With that, you should be putting the original under source control (or archived), so you can see and check the differences that the automated tool fixes (as a sanity check). Ambiguous cases should be fixed by hand.

Answer from reece on Stack Overflow
🌐
PYnative
pynative.com › home › python › json › validate json data using python
Validate JSON data using Python
May 14, 2021 - Check if a string is valid JSON in Python. Validate JSON Schema using Python. Validates incoming JSON data by checking if there all necessary fields present in JSON and also verify data types of those fields
Top answer
1 of 4
54

The built-in JSON module can be used as a validator:

import json

def parse(text):
    try:
        return json.loads(text)
    except ValueError as e:
        print('invalid json: %s' % e)
        return None # or: raise

You can make it work with files by using:

with open(filename) as f:
    return json.load(f)

instead of json.loads and you can include the filename as well in the error message.

On Python 3.3.5, for {test: "foo"}, I get:

invalid json: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

and on 2.7.6:

invalid json: Expecting property name: line 1 column 2 (char 1)

This is because the correct json is {"test": "foo"}.

When handling the invalid files, it is best to not process them any further. You can build a skipped.txt file listing the files with the error, so they can be checked and fixed by hand.

If possible, you should check the site/program that generated the invalid json files, fix that and then re-generate the json file. Otherwise, you are going to keep having new files that are invalid JSON.

Failing that, you will need to write a custom json parser that fixes common errors. With that, you should be putting the original under source control (or archived), so you can see and check the differences that the automated tool fixes (as a sanity check). Ambiguous cases should be fixed by hand.

2 of 4
3

Yes, there are ways to validate that a JSON file is valid. One way is to use a JSON parsing library that will throw exceptions if the input you provide is not well-formatted.

try:
   load_json_file(filename)
except InvalidDataException: # or something
   # oops guess it's not valid

Of course, if you want to fix it, you naturally cannot use a JSON loader since, well, it's not valid JSON in the first place. Unless the library you're using will automatically fix things for you, in which case you probably wouldn't even have this question.

One way is to load the file manually and tokenize it and attempt to detect errors and try to fix them as you go, but I'm sure there are cases where the error is just not possible to fix automatically and would be better off throwing an error and asking the user to fix their files.

I have not written a JSON fixer myself so I can't provide any details on how you might go about actually fixing errors.

However I am not sure whether it would be a good idea to fix all errors, since then you'd have assume your fixes are what the user actually wants. If it's a missing comma or they have an extra trailing comma, then that might be OK, but there may be cases where it is ambiguous what the user wants.

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-check-whether-a-string-is-valid-json-or-not
Python | Check whether a string is valid json or not - GeeksforGeeks
July 11, 2025 - In the below example, the string is an invalid JSON because, the string characters are enclosed in single quotes ('), but as per valid JSON schema, strings must be enclosed in double quotes ("). ... # Python code to demonstrate # checking whether string # is valid json or not import json ini_string = "{'akshat' : 1, 'nikhil' : 2}" # printing initial ini_string print ("initial string", ini_string) # checking for string try: json_object = json.loads(ini_string) print ("Is valid json?
🌐
Built In
builtin.com › software-engineering-perspectives › python-json-schema
How to Use JSON Schema to Validate JSON Documents in Python | Built In
In Python, we can use the jsonschema library to validate a JSON instance (also referred to as a JSON document as long as it’s unambiguous) against a schema.
🌐
Flexiple
flexiple.com › python › python-json-validator
Python JSON Validator - Flexiple
The JSON Validator is straightforward to use. We will demonstrate how to validate JSON data step-by-step. To get started, import the JSON module into your Python script:
🌐
PyPI
pypi.org › project › json-checker
json-checker · PyPI
If data is valid, Checker.validate will return the validated data · If data is invalid, Checker will raise CheckerError. If Checker(...) encounters a type (such as int, str), it will check if the corresponding piece of data is an instance of that type, otherwise it will raise CheckerError. >>> from json_checker import Checker >>> Checker(int).validate(123) 123 >>> Checker(int).validate('123') Traceback (most recent call last): ...
      » pip install json-checker
    
Published   Aug 31, 2019
Version   2.0.0
🌐
Couchbase
couchbase.com › home › validate json documents in python using pydantic
Validate JSON Documents in Python using Pydantic
June 14, 2025 - This user profile example shows how we can easily create custom schemas for our JSON documents. This post also shows how to use the test and validate capabilities of Python and the pydantic module.
Find elsewhere
🌐
Testcookbook
testcookbook.com › book › python › json-schema-validation.html
JSON Schema Validation - Test Cookbook
import json def validate_json_syntax(d): try: return json.loads(d) except ValueError: print('DEBUG: JSON data contains an error') return False #will return the data data = '{"firstName": "John", "lastName": "Doe"}' print validate_json_syntax(data) #will return false because JSON not valid #missing quotes around lastName data = '{"firstName": "John", lastName: "Doe"}' print validate_json_syntax(data) Run the test to see what output looks like. ... $ python test.py {u'lastName': u'Doe', u'firstName': u'John'} DEBUG: JSON data contains an error False
🌐
GitHub
github.com › donofden › python-validate-json-schema
GitHub - donofden/python-validate-json-schema: Validate JSON Schema using Python
This is a helper project for JSON Schema validation in Python, which uses Jsonschema the most complete and compliant JSON Schema validator. Validate the necessary fields present in JSON file
Starred by 6 users
Forked by 2 users
Languages   Python 100.0% | Python 100.0%
🌐
Donofden
donofden.com › blog › 2020 › 03 › 15 › How-to-Validate-JSON-Schema-using-Python
How to Validate JSON Schema using Python
jsonschema is an implementation of JSON Schema for Python. Using jsonschema, we can create a schema of our choice, so every time we can validate the JSON document against this schema, if it passed, we could say that the JSON document is valid.
🌐
JSONLint
jsonlint.com
JSONLint - The JSON Validator
The best way to find and correct errors while simultaneously saving time is to use an online tool such as JSONLint. JSONLint will check the validity of your JSON code, detect and point out line numbers of the code containing errors.
🌐
YouTube
youtube.com › watch
How to validate Json format in python - YouTube
How to validate Json format in pythonPython JSON validatorhttps://www.w3schools.com/python/python_json.asp
Published   July 19, 2022
🌐
Towards Data Science
towardsdatascience.com › home › latest › how to use json schema to validate json documents in python
How to Use JSON Schema to Validate JSON Documents in Python | Towards Data Science
March 5, 2025 - In Python, we can use the jsonschema library to validate a JSON instance (can also be referred to as JSON document as long as it’s unambiguous) against a schema.
🌐
Soumendrak
blog.soumendrak.com › how-to-validate-a-json-in-python
Soumendra Kumar Sahoo • How to validate a JSON in Python
January 13, 2024 - JSONSchema validation is a powerful tool for validating JSON data. It helps ensure that the data you receive conforms to a particular schema or structure. With JSONSchema validation, you can define the expected format of the JSON data and then ...
🌐
jsonschema
python-jsonschema.readthedocs.io
jsonschema 4.26.0 documentation
>>> validate(instance={"name" : ... ... ValidationError: 'Invalid' is not of type 'number' It can also be used from the command line by installing check-jsonschema....
🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
August 20, 2025 - To swiftly check if a JSON file is valid, you can leverage Python’s json.tool. You can run the json.tool module as an executable in the terminal using the -m switch.
🌐
PyPI
pypi.org › project › jsonschema
jsonschema · PyPI
An implementation of JSON Schema validation for Python
      » pip install jsonschema
    
Published   Jan 07, 2026
Version   4.26.0
🌐
Python Forum
python-forum.io › thread-24649.html
Validate JSON file
Hi, I am new to the python language (well new to any programming language). From what I have been reding on-line, python has components to validate JSON files, which is what I need to do. In stalled Python 3.81 and found a script on-line that says i...
🌐
jsonschema
python-jsonschema.readthedocs.io › en › latest › validate
Schema Validation - jsonschema 4.26.1.dev25+gad0a1b301 documentation
The Basics: The simplest way to validate an instance under a given schema is to use the validate function. The Validator Protocol: jsonschema defines a protocol that all validator classes adhere to...