You can validate JSON using an online tool, the command-line (jq or Python), JavaScript's JSON.parse() in a try/catch, Python's json.loads(), or IDE extensions. Each method suits a different workflow. For quick checks, use an online JSON validator. For automation, use the command line or a language method.
- All 5 methods are free — no paid tools needed to validate JSON syntax.
- The most common JSON error is a trailing comma after the last item in an object or array, which is not allowed by RFC 8259.
python -m json.toolis the fastest CLI method on any system with Python installed — no extra packages needed.- JSON.parse() throws a SyntaxError with the exact character position of the error, making it useful for debugging.
- IDE extensions like Prettier and ESLint validate JSON on every save without any manual step.
Why Does JSON Validation Matter?
Invalid JSON causes silent failures. An API returning malformed JSON will crash the client application with an unhelpful error. A misconfigured package.json with a trailing comma will prevent npm install from running. A broken webhook payload will fail silently and lose data.
JSON is defined in RFC 8259, which specifies an exact grammar. The rules are strict: no trailing commas, no comments, all strings must use double quotes, and numbers cannot have leading zeros. Any deviation makes the entire document invalid.
Method 1: Online JSON Validator (Fastest for One-Off Checks)
An online validator is the fastest way to check JSON when you are not in a coding environment or working in a browser. Paste your JSON and the tool highlights errors with line numbers and error descriptions.
- Go to ToolSparkr's JSON Formatter & Validator.
- Paste your JSON into the input field.
- Click Format / Validate.
- If valid, your JSON is formatted with proper indentation. If invalid, an error message with the position of the problem is shown.
- Use the Minify button to remove whitespace for production use.
This method requires no installation and works on any device. It is ideal for validating API responses you have copied from browser DevTools or Postman.
Method 2: Command Line with Python (No Extra Packages)
If you have Python installed (Python 3 is available by default on macOS and most Linux distributions), you can validate JSON without installing anything extra:
echo '{"name":"Alice","active":true}' | python3 -m json.tool
If valid, Python prints the formatted JSON. If invalid, it prints the error:
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 18 (char 17)
To validate a file:
python3 -m json.tool myfile.json > /dev/null && echo "Valid" || echo "Invalid"
The > /dev/null suppresses the formatted output so you only see the pass/fail message.
Method 3: Command Line with jq (More Powerful)
jq is a command-line JSON processor available for Linux, macOS, and Windows. It validates JSON and also lets you query and transform it.
- Install jq:
brew install jq(macOS) orsudo apt install jq(Ubuntu/Debian). - Validate a file:
jq . myfile.json > /dev/null - If invalid, jq prints the parse error with the line and column number.
- Validate a URL response:
curl -s https://api.example.com/data | jq .
jq is the best command-line tool for validation when you also need to extract specific fields or transform the data.
Method 4: JavaScript JSON.parse() in a Try/Catch
JSON.parse() throws a SyntaxError if the input is not valid JSON. Wrap it in a try/catch to handle the error gracefully:
function validateJSON(text) {
try {
const parsed = JSON.parse(text);
console.log('Valid JSON:', parsed);
return { valid: true, data: parsed };
} catch (err) {
console.error('Invalid JSON:', err.message);
return { valid: false, error: err.message };
}
}
// Example usage
const result = validateJSON('{"name":"Alice","active":true}');
// Valid JSON: { name: 'Alice', active: true }
const bad = validateJSON('{"name":"Alice",}');
// Invalid JSON: Unexpected token } in JSON at position 17
import json
def validate_json(text):
try:
data = json.loads(text)
print('Valid JSON:', data)
return {'valid': True, 'data': data}
except json.JSONDecodeError as e:
print(f'Invalid JSON: {e.msg} at line {e.lineno}, col {e.colno}')
return {'valid': False, 'error': str(e)}
# Example usage
validate_json('{"name": "Alice", "active": true}')
# Valid JSON: {'name': 'Alice', 'active': True}
validate_json('{"name": "Alice",}')
# Invalid JSON: Expecting property name enclosed in double quotes at line 1, col 19
The JavaScript error message includes the character position. The Python JSONDecodeError includes line number, column number, and the specific problem. Both are suitable for use in production code that processes external JSON input.
Method 5: IDE Extensions (Continuous Validation)
IDE extensions validate JSON as you type, highlighting errors before you save or run the code. This is the most efficient approach for everyday development work.
- VS Code: JSON validation is built-in. Open any
.jsonfile and VS Code underlines errors automatically. No extension needed for basic validation. - Prettier: Install the Prettier extension and enable
Format on Save. Prettier will refuse to format invalid JSON, making invalid files immediately visible. - ESLint with json plugin: Install
eslint-plugin-jsonto lint JSON files alongside JavaScript. Useful in monorepos. - JetBrains IDEs (IntelliJ, WebStorm, PhpStorm): JSON validation and schema validation are built-in. Right-click any JSON file and select Validate to check against a JSON Schema.
- JSON Schema: In VS Code, add a
$schemakey to your JSON file pointing to a schema URL (e.g.,https://json.schemastore.org/package). VS Code will validate all keys and values against the schema, not just syntax.
What Are the Most Common JSON Syntax Errors?
| Error | Invalid Example | Fix |
|---|---|---|
| Trailing comma | {"a":1,} |
Remove the comma before } |
| Single quotes | {'key':'value'} |
Use double quotes: {"key":"value"} |
| Unquoted keys | {key: "value"} |
Quote all keys: {"key":"value"} |
| Comments | {"a":1 // comment} |
Remove comments — JSON does not support them |
| Undefined value | {"a": undefined} |
Use null instead of undefined |
| NaN or Infinity | {"a": NaN} |
Use null or a numeric string |
Validate Your JSON Now
Use the free JSON Formatter & Validator to paste, validate, and format any JSON in seconds. The tool shows the exact line and column of any error.
python3 -m json.tool filename.json > /dev/null or jq . filename.json > /dev/null in a pipeline step. These commands exit with code 1 if the JSON is invalid, which will fail the build. For JSON Schema validation in CI, use ajv-cli (Node.js) or jsonschema (Python).} before the parser expects it, single quotes appear where double quotes are required, or a JavaScript keyword like undefined appears where a JSON value is expected.tsconfig.json and settings.json. Standard validators will reject JSONC because of the comments. To validate JSONC programmatically, use the jsonc-parser npm package, which strips comments before parsing.