How to Validate JSON: Syntax Checking & Schema Strategies
Learn how to validate JSON data online, programmatically in JavaScript, Python, and Go, and using JSON Schema ($schema) for robust API contracts.
Why JSON Validation Is Critical
Malformed JSON payloads cause runtime exceptions, broken API integrations, and failed deployments. Validating JSON ensures structural correctness before data enters mission-critical pipelines.
Programmatic Validation Examples
1. JavaScript / Node.js
function isValidJson(jsonString) {
try {
JSON.parse(jsonString);
return true;
} catch (error) {
console.error("Syntax Error at:", error.message);
return false;
}
}
2. Python 3
import json
def validate_payload(raw_text):
try:
data = json.loads(raw_text)
return True, data
except json.JSONDecodeError as err:
return False, f"Error on line {err.lineno}, col {err.colno}: {err.msg}"
Syntax Validation vs. Schema Validation
Syntax Validation checks whether the text follows RFC 8259 syntax rules (matching braces, valid quotes, proper commas).
Schema Validation (using JSON Schema) checks semantic data types, required fields, minimum/maximum numeric ranges, and regex string patterns:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"userId": { "type": "integer" },
"email": { "type": "string", "format": "email" }
},
"required": ["userId", "email"]
}