How to validate JSON from the command line on Linux

Last updated on November 25, 2020 by Dan Nanni

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---

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean