How to validate JSON from the command line on Linux

Due to its syntactic simplicity and flexibility, JSON (JavaScript Object Notation) has become pretty much the de-facto standard data exchange format used in many web applications. As JSON becomes widely used to represent structured data with a great degree of flexibility, the need arises for being able to "validate" JSON representations.

Thus there came something called JSON schema (currently IETF draft v4), which specifies the structure of JSON data in a declarative format, much like a database schema defines the logical structure and relationship of database records. JSON schema itself is written in JSON. JSON schema can be quite useful not only to generate properly formatted JSON data, but also to check the correctness of the structure and format of JSON data.

This tutorial will cover two aspects of JSON validation with respect to JSON schema. First, you can validate JSON data against a JSON schema, i.e., checking whether the given JSON data conform to the JSON schema specification. The other aspect of validation is schema validation, where you check whether the JSON schema itself is well-formed per the JSON schema Internet draft. In both cases, I will show how to perform necessary validation from the command line on Linux.

Validate JSON data against a JSON Schema from the Command Line

There are many open-source libraries and standalone tools that can validate JSON data. One of them is JSON-Spec python library which comes with a command-line tool called json.

To install JSON-Spec on Linux, first install pip, and then install it as follows.

$ sudo pip install json-spec

Then you can validate JSON data against a JSON schema in the form of:

$ json validate --schema-file=schema.json --document-file=data.json
$ json validate --schema-file=schema.json < data.json
$ json validate --schema-file=schema.json --document-json='{"foo": ["bar", "baz"]}'

Let's use the following sample JSON data and JSON schema, and see how the JSON data can be validated against the JSON schema.

data.json:

{
    "id" : 1,
    "name": "Dan Nanni",
    "age": 25,
    "skills": ["shell scripting", "data center"]
}

schema.json:

{
    "title": "Employee",
    "description": "Employee description",
    "type": "object",
    "properties": {
        "id": {
            "description": "The unique identifier of an employee",
            "type": "integer"
        },
        "name": {
            "description": "Name of the employee",
            "type": "string"
        },
        "age": {
            "type": "number",
            "minimum": 0,
            "exclusiveMinimum": true
        },
        "skills": {
            "type": "array",
            "items": {
                "type": "string"
            },
            "minItems": 1,
            "uniqueItems": true
        }
    },
    "required": ["id", "name", "age"]
}

Given the above samples, validate data.json against schema.json as follows.

$ json validate --schema-file=schema.json --document-file=data.json

If the JSON data conforms to the schema, the command will produce nothing. If not, the command will raise an exception:

Exception: document does not validate with schema.

  #/
    - reason Missing property

Validate a JSON Schema from the Command Line

Another aspect of JSON validation is to check whether or not the JSON schema itself has valid syntax. For schema validation, a Java tool called json-schema-validator comes in handy. This tool supports the syntax validation for the latest JSON schema draft v4. It can work standalone from the command line, or can be integrated into Maven build process as a plugin.

To run it from the command line, you can download its fully executable JAR version from Bintray. Make sure to download json-schema-validator-X.X.X-lib.jar, which has all dependencies incorporated.

Once you download it, check the validity of a JSON schema from the command line as follows.

$ java -jar json-schema-validator-2.2.6-lib.jar --syntax schema.json
--- BEGIN /home/dan/schema.json---
validation: SUCCESS
--- END /home/dan/schema.json---

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

4 thoughts on “How to validate JSON from the command line on Linux

  1. Another tool worth noting in this, although not related as much to validation, is "jq" - (https://stedolan.github.io/jq/). I have compiled and used it on a bunch of projects that needed to parse JSON data on the command line. Haven't used it to validate schema, but it is a great tool!!

  2. so you're telling me that you need to write more JSON to validate JSON? Also, this is not about validating that the JSON is correctly formatted in the JSON format. This is more like a database schema, like in SQL or something.
    When I saw the post I thought it was going to talk about python's json.tool:
    cat | python -m json.tool
    This will error out if it is not valid JSON, or if it was valid then pretty print the JSON for you. I imagine you could make a python one liner to do a check for you. Something like:
    python -c "import json,sys; txt=sys.stdin.read(); json.loads(txt)"
    If it can't decode what you gave it, the program will traceback and return error code 1 (on my machine at least).

  3. A suggestion, it's confusing that the two JSON examples are named "data.json" and "schema.json" but then later in the actual command

    json validate --schema-file=test.schema --document-file=test.json

    they are renamed. I copied the two jsons and mouse copied the json validate command and was suprised by the "file not found" errors

Leave a comment

Your email address will not be published. Required fields are marked *