JSON Validator
Validate JSON syntax and identify errors in your JSON data. This tool checks for proper formatting, missing brackets, invalid characters, and other common JSON mistakes.
Common JSON Errors
| Error | Cause | Fix |
|---|---|---|
| Unexpected token | Syntax error | Check for typos |
| Unterminated string | Missing quote | Close all strings |
| Trailing comma | Comma after last item | Remove trailing comma |
| Invalid character | Non-JSON character | Use proper escaping |
| Missing colon | Key without value | Add : after key |
Valid JSON Rules
``javascript
// Keys must be strings with double quotes
{ "name": "John" } // ✓ Valid
{ name: "John" } // ✗ Invalid
// Strings must use double quotes
{ "name": "John" } // ✓ Valid
{ "name": 'John' } // ✗ Invalid
// No trailing commas
{ "a": 1, "b": 2 } // ✓ Valid
{ "a": 1, "b": 2, } // ✗ Invalid
// No comments
{ "a": 1 } // ✓ Valid
{ "a": 1 } // note // ✗ Invalid
`
Validation Code
`javascript
function validateJSON(jsonString) {
try {
JSON.parse(jsonString);
return { valid: true };
} catch (error) {
return {
valid: false,
error: error.message,
position: error.message.match(/position (\d+)/)?.[1]
};
}
}
// Example
const result = validateJSON('{"name": "John", }');
// { valid: false, error: "Unexpected token }", position: 17 }
``
JSON Data Types
| Type | Example | Notes |
|---|---|---|
| String | "hello" | Double quotes only |
| Number | 42, 3.14 | No quotes, no leading zeros |
| Boolean | true, false | Lowercase only |
| Null | null | Lowercase only |
| Array | [1, 2, 3] | Square brackets |
| Object | {"a": 1} | Curly braces |