You paste some JSON into your app and get an error like Unexpected token. Or an API rejects your request with “invalid JSON”. Somewhere in the text is a mistake. But where?
This guide explains what “valid JSON” means. Then it shows simple ways to check it: online, in JavaScript, in Python and from the command line.
Quick answer: To check if JSON is valid, paste it into a JSON validator. It tells you if the text follows the JSON rules. If not, it shows the line and column of the error. In code, try to read it with JSON.parse() or Python’s json.loads() and catch the error.
What does “valid JSON” mean?
Valid JSON is text that follows the JSON standard exactly. A program called a parser reads the text and turns it into data. If the text breaks even one rule, the parser stops and reports an error.
The main rules are:
- Keys and strings use double quotes, like
"name": "Ada". - Items are separated by commas, with no comma after the last item.
- Every
{and[has a matching}and]. - Values can only be strings, numbers,
true,false,null, objects or arrays. - There are no comments, and no
undefined,NaNorInfinity.
Here is some invalid JSON with three mistakes:
{
'name': 'Ada',
"age": 36
"skills": ["math", "poetry",]
}It uses single quotes. It is missing a comma after 36. And it has a comma after the last item in the list. Here is the fixed version:
{
"name": "Ada",
"age": 36,
"skills": ["math", "poetry"]
}For more examples, see the most common JSON errors and how to fix them.
How to validate JSON online
- Open the JSON Validator.
- Paste your JSON, upload a file, or drag one onto the editor.
- The status bar turns green if the JSON is valid. If not, it shows the line, the column and the cause of the first error.
- Click “Go to error”, fix the problem and keep typing. The check updates as you type.
The messages are in plain English, such as “Trailing comma before } is not allowed”. That is much easier to act on than a message like “Unexpected token at position 312”.
For a deeper check, use the JSON Error Analyzer. It explains the likely cause of an error and how to fix it. It also warns about problems that cause bugs later, even when the JSON reads without errors. Examples are the same key used twice, or numbers too large for JavaScript.
How to check if JSON is valid in JavaScript
JSON.parse() throws an error when the text is not valid JSON. Put it inside try/catch to check it safely:
function isValidJson(text) {
try {
JSON.parse(text);
return true;
} catch (error) {
console.log(error.message);
return false;
}
}
isValidJson('{"name": "Ada"}'); // true
isValidJson("{'name': 'Ada'}"); // falseThe error message is different in each browser, and some don’t say where the error is. If you need the exact line and column, paste the text into a validator.
Note that JSON.parse("42") and JSON.parse('"hello"') also work. A single number or string is valid JSON on its own. If you expect an object, check the type after parsing.
How to validate JSON in Python
In Python, json.loads() gives a json.JSONDecodeError for invalid JSON. The error tells you the line and column:
import json
def is_valid_json(text):
try:
json.loads(text)
return True
except json.JSONDecodeError as e:
print(f"Line {e.lineno}, column {e.colno}: {e.msg}")
return FalseTo check a file, use json.load() with an open file instead. One thing to know: by default, Python accepts NaN and Infinity. These are not valid JSON. So a file that passes in Python can still fail in other languages.
How to validate a JSON file from the command line
python -m json.tool data.jsonIf the file is valid, this prints it formatted. If not, it prints an error such as Expecting ',' delimiter: line 4 column 3. On some systems, type python3 instead of python.
Valid JSON is not always correct JSON
A validator checks the syntax, which means the grammar of the text. It does not check whether the data makes sense. This JSON is perfectly valid:
{ "name": "", "age": "thirty-six", "email": 42 }But your app probably expects a name that is not empty. It expects an age that is a number, and an email that is text. To check that, you need a JSON Schema. A schema is a separate JSON document that describes what your data should look like:
{
"type": "object",
"required": ["name", "age", "email"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 0 },
"email": { "type": "string", "format": "email" }
}
}Paste your data and the schema into the JSON Schema Validator. It lists every problem with its path and a clear explanation. New to schemas? Read What Is JSON Schema?
Common questions
How do I know if my JSON is valid?
Paste it into a JSON validator. If it reads without errors, it is valid. If not, the validator shows where the first error is.
Why does my JSON say “Unexpected token”?
The parser found a character it did not expect at that spot. Common causes are single quotes, an extra comma at the end, a missing comma or a comment. The real mistake is often just before the position in the message.
Can I fix invalid JSON automatically?
Often, yes. JSON Repair fixes common problems like single quotes, extra commas and missing brackets. It also lists every change. Check the result, because a repair is a best guess.
Is JSON with comments valid?
No. Standard JSON does not allow comments. Some files, like VS Code settings, use JSONC. It is a version of JSON that does allow them. See JSON and comments to learn more.