How can I catch new posts?
Bookmark this blog and watch the homepage and tools hubβwe surface new guides there. No account or mailing list is required to read articles.
Learn how to use Python's built-in json module and third-party jsonschema library to validate JSON format and structure. Includes complete code examples and best practices.
In web development and data interaction, JSON (JavaScript Object Notation) is one of the most commonly used data formats.
It's used for API communication, configuration files, log storage, and more.
However, during development, we often encounter:
These errors can prevent programs from parsing JSON data, so before using it in production, we need to validate that the JSON format is correct.
Python has a very practical built-in module called json that can easily handle JSON loading and validation.
import json
def validate_json(json_string):
try:
json.loads(json_string)
print("β
JSON format is correct!")
except json.JSONDecodeError as e:
print("β JSON format error:", e)
# Example JSON
data = '{"name": "Alice", "age": 25}'
validate_json(data)Output:
β
JSON format is correct!If the input format is incorrect, for example:
data = '{"name": "Alice", "age": 25,}' # Extra comma
validate_json(data)Result:
β JSON format error: Expecting property name enclosed in double quotes: line 1 column 28 (char 27)import json
def validate_json_file(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
try:
json.load(f)
print(f"β
JSON format in file {file_path} is correct!")
except json.JSONDecodeError as e:
print(f"β {file_path} format error: {e}")
# Usage
validate_json_file('data.json')This script will automatically read the file and validate whether the content is valid JSON.
Beyond syntax correctness, sometimes we also want to validate that the JSON structure meets expectations, such as requiring name, age, and email fields.
We can use the third-party library jsonschema for this.
pip install jsonschemafrom jsonschema import validate, ValidationError
# Define data structure
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"email": {"type": "string"}
},
"required": ["name", "age"]
}
# Test data
data = {"name": "Alice", "age": 25}
try:
validate(instance=data, schema=schema)
print("β
JSON data structure is valid!")
except ValidationError as e:
print("β JSON structure error:", e.message)Output:
β
JSON data structure is valid!If fields are missing or types are incorrect, clear error messages will be displayed.
import os
import json
def validate_json_files(folder):
for file in os.listdir(folder):
if file.endswith('.json'):
path = os.path.join(folder, file)
try:
with open(path, 'r', encoding='utf-8') as f:
json.load(f)
print(f"β
{file} validation passed")
except json.JSONDecodeError as e:
print(f"β {file} format error: {e}")
# Batch validate all JSON files in current directory
validate_json_files(".")Using Python to validate JSON is not only simple and efficient, but can also be integrated with automated testing, data import, and other workflows.
You can use JSON validation as an independent step in your project to avoid interface exceptions caused by format errors in production environments.
If you don't want to run scripts manually, you can also use our online tool directly:
π JSON Validator Online Tool
It supports:
Dedicated to providing developers with the best JSON processing tools
More posts coming soon...
Back to BlogFollowing the blog, topics we cover, and how to suggest guides.
Bookmark this blog and watch the homepage and tools hubβwe surface new guides there. No account or mailing list is required to read articles.
JSON validation, formatting, conversion, debugging workflows, and JSON Work releasesβmapped to what the free on-site tools can do locally in your browser.
Yes. Reach out via the About page or GitHub; we prioritize guides tied to real integration and debugging scenarios.