Is JSON valid? Validate JSON online

| 4 min read

Is JSON valid? Why is JSON invalid? How do you check if JSON is valid or not? You may get a vague, indirect errors if there is something wrong with your JSON data. If your JSON document does not contain indentation, it can be hard to spot the cause of the issue. In this article you will learn how you can validate JSON data, learn how it works, and learn what tools you can use for this.

Validate JSON online

Use this free JSON validator to check whether the syntax of your JSON is valid:

loading...

Validating JSON syntax or structure

There are two levels on which you can validate your JSON documents:

  1. Validate the syntax of a JSON document
  2. Validate the structure of a JSON document against a schema

In general, these two categories are both called “validating JSON”, which can be confusing. We’ll clarify them here.

Validate JSON syntax

Validating the syntax makes sure the JSON document contains valid JSON and can be parsed. For example, forgetting a comma between two items in an array makes the JSON invalid. Trying to parse the document will result in a syntax error. For example the following document is invalid because it misses a comma between the first and the second key-value pair:

{
  "name": "Joe"
  "age": 42
}

Sometimes, the error is easy to spot, but in larger documents that are not formatted it can be hard to debug what is wrong. Also, some errors are hard to spot by the eye, like having a curly double quote character instead of a straight one:

{
  “Name”: “Joe”,
  “age”: 42
}

Typical causes of invalid JSON include copying JSON from a Word document, dealing with a JSON dialect like JSON5, and having a truncated JSON due to some text size limit. Another common case is having a JavaScript object: JSON is valid JavaScript, but JavaScript is not always valid JSON, since for example the quotes for object keys are optional in JavaScript but required in JSON. All common issues and solutions are described in the article “How to fix JSON and validate it with ease”.

Validate JSON structure

Besides syntax errors, it can be that you want to verify whether the contents of a document is according to a specific data structure or a “schema”. For example, given the following document:

{
  "name": "Joe",
  "age": 42
}

How can you make sure the document is an object having properties “name” and “age”, where “name” is a string, and “age” is an integer number? How can you validate the structure? This can be done using a validation library. One proven solution is to use JSON Schema, a language to describe the structure of a JSON document. The JSON Schema for the example at hand can look like:

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" }
  },
  "required": ["name", "age"]
}

The article “JSON Schema validator: a powerful way to check JSON data” explains in detail what JSON Schema is and how and when to use it.

When to validate JSON?

You may need to manually validate a JSON document when you’re debugging an issue in your code as a developer. In such a case you can use online tools like JSON Editor Online.

When you need to programmatically validate JSON, this is normally best to do “at the gate”: when the data enters your application. For example when fetching data in a web application, or receiving JSON in a backend server via a REST API. Best is to directly check the data, so you don’t have to reckon with potentially invalid data in the application itself.

Is Json Valid conclusion

So, what makes a JSON valid? We have learned that we can look at two things. The first is validating the syntax of a JSON document, like missing quotes or commas. The second is validating the structure of a JSON document, such as verifying that the document is an object containing properties like “name” and “age”.

Validation of these two categories requires different tools. Validating JSON syntax requires a strict JSON parser with helpful error messages, else debugging an issue can be difficult. A tool like JSON Editor Online (and the inline editor in this article) can help repair syntax errors automatically. Validating JSON structure requires a validation library, such as Ajv with JSON Schema.