A JSON validator answers a simple but high-stakes question: is this string valid JSON? If your config file is rejected by your build tool, if your API returns 400 because of a malformed request body, or if your CI pipeline fails because package.json won't parse — you need a fast, accurate JSON linter with precise error reporting. This validator returns the exact line, column, and character position of the first syntax error, so you can fix the problem in seconds instead of hunting through hundreds of lines of nested objects.
How JSON Validation Works
Validation uses the browser's native JSON.parse(), which implements the RFC 8259 grammar strictly. The validator catches every common mistake: trailing commas after the last array element or object property, unquoted object keys, single-quoted strings, unescaped control characters inside strings, missing closing brackets, duplicate keys (technically allowed by the spec but flagged), and unterminated strings. When parsing fails, the error message includes the position offset which we convert to a line/column pair for easier debugging.
When You Need a JSON Linter
- CI/CD pipelines — verify config files parse before deploying.
- API request debugging — confirm the body you're POSTing is well-formed.
- Database imports — MongoDB, CouchDB, Elasticsearch bulk imports all require valid JSON.
- Migrations — converting YAML configs to JSON? Validate after each conversion.
- Schema validation — structural validity is step one; JSON Schema validation (against a
.schema.json) is step two.
JSON Validation in Code
// Browser / Node.js
function isValidJSON(str) {
try { JSON.parse(str); return true; }
catch { return false; }
}
// With detailed error info
function validateJSON(str) {
try {
JSON.parse(str);
return { valid: true };
} catch (e) {
const match = e.message.match(/position (\d+)/);
return {
valid: false,
error: e.message,
position: match ? parseInt(match[1]) : null,
};
}
}
For CLI: jq empty file.json exits 0 if valid, non-zero with a parse error if not. python -c "import json,sys; json.load(sys.stdin)" < file.json works similarly.
Common JSON Errors & Fixes
Trailing comma after last array element: [1, 2, 3,] → remove the comma. Single quotes: {'name': 'value'} → use double quotes. Unquoted keys: {name: "value"} → quote the key. Comments: JSON doesn't allow // or /* */ — if you need them, use JSON5 or JSONC and strip before parsing. Multi-line strings: use \n escape sequences instead of literal newlines. After validating, you might want to format the JSON or minify it for production.