In this article, we will see how to validate JSON data using Python. There are multiple scenarios where we need different types of JSON validation. In this article, we will cover the followings
- Check if a string is valid JSON in Python: Here we can check if a string is valid JSON before parsing it. For example, if you are working with any API, what if it returns Invalid JSON or any other data? Here, we will validate JSON as per the standard convention format.
- Validate JSON Schema using Python: Here we will see how to validates incoming JSON data by checking if there all necessary fields present in JSON and also validate data types of those fields.
Further Reading:
- Solve Python JSON Exercise to practice Python JSON skills

Check if a string is valid JSON in Python
When we receive the JSON response from any API, we must validate it before performing any operation using that data. There are various ways to validate JSON as per the standard convention format.
Using json.loads and json.load() method:
As we know, the json module provides two methods to parse JSON data using Python.
json.loads()
: To parse JSON from String.json.load()
to Parse JSON from a file.
Both methods will throw a ValueError if the string or data you pass can’t be decoded as JSON. When we receive a JSON response, we can pass it to the json.loads()
method to validate it as per the standard convention. Let’s see the example.
import json
def validateJSON(jsonData):
try:
json.loads(jsonData)
except ValueError as err:
return False
return True
InvalidJsonData = """{"name": "jane doe", "salary": 9000, "email": "jane.doe@pynative.com",}"""
isValid = validateJSON(InvalidJsonData)
print("Given JSON string is Valid", isValid)
validJsonData = """{"name": "jane doe", "salary": 9000, "email": "jane.doe@pynative.com"}"""
isValid = validateJSON(validJsonData)
print("Given JSON string is Valid", isValid)
Output:
Given JSON string is Valid False Given JSON string is Valid True
- As you can see in the first example, we passed an invalid JSON string to the load method. In the first JSON data, we have added an extra comma to make it invalid because of this
json.loads
method generated a valueError. - In the second call, we passed a valid JSON document, and it successfully parsed by the
json.loads
method.
Note: Use json.load()
method instead of json.loads()
to parse and validate JSON from a file.
Validate JSON Object from the command line before writing it in a file
Python provides The json.tool
module to validate JSON objects from the command line. When we send JSON response to a client or when we write JSON data to file we need to make sure that we write validated data into a file
Run a below command on the command line. Here we are validating the Python dictionary in a JSON formatted string.
echo {"id": 1, "item": "itemXyz"} | python -m json.tool
Output:
{ "id": 1, "name": "itemXyz" }
Let’s pass an invalid object for JSON validating.
echo {"id": 1 "name": "Jessa Duggar"} | python -m json.tool
Output:
Expecting ',' delimiter: line 1 column 10 (char 9)
We can also use different command-line options of json.tool
module to validate JSON. Let’s see those
Validate JSON File
Let’s assume that you want to parse a JSON file using Python. Instead of directly parsing it, we need to validate it so that we can assure that file content is a valid JSON document. Let’s see how to use a command-line option of a json.tool
module to validate file containing JSON data.
File content before running the following command

Command:
python -m json.tool fileName
Example:
python -m json.tool studentDetails.json
We received the following error because the file content is not in JSON format.
error expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Executed the same command after correcting an error
Output:
{ "id": 1, "name": "Jessa Duggar", "class": 9, "attendance": 75, "subjects": [ "English", "Geometry", "Physics", "World History" ], "email": "jess@example.com" }
Validate JSON Schema using Python
Sometimes we need something extra than just a standard JSON validation. i.e., We will see how to validate incoming JSON data by checking all necessary fields present in JSON file or response and also validate data types of those fields.
Such a scenario includes the following things:
- We need the necessary fields present in JSON file
- We need data of a JSON filed in a type that we want. For example, we want all numeric fields in the number format instead of number encoded in a string format like this
Marks: "75"
so we can use it directly instead of checking and converting it every time.
We need to use the jsonschema
library. This library is useful for validating JSON data. The library uses the format to make validations based on the given schema. 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.
Follow the below steps:
- First, install jsonschema using pip command.
pip install jsonschema
- Define Schema: Describe what kind of JSON you expect
- Convert JSON to Python Object using
json.load
orjson.loads
methods. - Pass resultant JSON to
validate()
method of ajsonschema
. This method will raise an exception if given json is not what is described in the schema.
Let’s see the example. In this example, I am validating student JSON. The following conditions must meet to call it as a valid JSON
- The student name and roll number must be present in JSON data.
- Marks and roll number must be in a number format.
import json
import jsonschema
from jsonschema import validate
# Describe what kind of json you expect.
studentSchema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"rollnumber": {"type": "number"},
"marks": {"type": "number"},
},
}
def validateJson(jsonData):
try:
validate(instance=jsonData, schema=studentSchema)
except jsonschema.exceptions.ValidationError as err:
return False
return True
# Convert json to python object.
jsonData = json.loads('{"name": "jane doe", "rollnumber": "25", "marks": 72}')
# validate it
isValid = validateJson(jsonData)
if isValid:
print(jsonData)
print("Given JSON data is Valid")
else:
print(jsonData)
print("Given JSON data is InValid")
# Convert json to python object.
jsonData = json.loads('{"name": "jane doe", "rollnumber": 25, "marks": 72}')
# validate it
isValid = validateJson(jsonData)
if isValid:
print(jsonData)
print("Given JSON data is Valid")
else:
print(jsonData)
print("Given JSON data is InValid")
Output:
{'name': 'jane doe', 'rollnumber': '25', 'marks': 72} Given JSON data is InValid {'name': 'jane doe', 'rollnumber': 25, 'marks': 72} Given JSON data is Valid
Note: The validate()
method will raise an exception if given JSON is not what is described in the schema.
The first JSON data contains roll number value in string format instead of a number so when we called validate() method it returned False. If print exception it will show like this.
Failed validating 'type' in schema['properties']['rollnumber']:
{'type': 'number'}
On instance['rollnumber']:
'25'