Linux Commands

How to Validate JSON from the Command Line on Linux

JSON (JavaScript Object Notation) is a format used for exchanging data between web applications. On the other hand, the JSON schema is a standard that defines the structure and content of a JSON document, which includes expected properties, data types, and other constraints for a valid JSON document. Validating JSON is useful since it ensures that the data transferred between any two systems is in correct format.

If you are searching for ways to validate JSON from the Linux command line, follow this article’s guidelines.

Validate JSON from the Command Line on Linux

There are different ways to validate JSON from the command line on Linux. Here are three possible methods:

Method 1: Validating JSON file Using JSON-Spec Command

Many libraries and open-source tools are there to validate JSON files. JSON Spec is one of those commands that can effectively validate the JSON data against a JSON schema. However, you must install JSON Spec on Linux from the pip command, which can be installed from the following command:

sudo apt install pip

After installing pip on Linux, run the following command to install JSON Spec.

sudo pip install json-spec

Now we will use two JSON files named data.json and schema.json. Using the JSON Spec python library tool, we will validate our JSON files.

Now run the following command that will validate JSON data against a JSON schema:

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

This is a command line instruction to validate a JSON file called “data.json” against a JSON schema defined in “schema.json“.

The “json validate” command is likely a specific tool or library designed for JSON schema validation. By using the –schema-file flag followed by the name of the JSON schema file and the –document-file flag followed by the name of the JSON document file, the tool can validate that the document completely matched the schema’s file specifications.

If the JSON document passes the validation, it means that it is structurally and semantically correct according to the schema. If it fails the validation, the tool will likely provide error messages indicating which aspects of the document are not compliant with the schema.

Here is another syntax that can validate JSON files:

json validate --schema-file=schema.json < data.json

This command can also validate a JSON file called “data.json” against a JSON schema defined in “schema.json“.

In this command, the “<” character is used to redirect the contents of the “data.json” file to the standard input of the validation tool. This is an alternative to using the “–document-file” flag in the previous example.

This command will compare the JSON document against the schema specified in “schema.json” to ensure that both have the same syntax.

If both JSON files match structurally and semantically, the command will output nothing; however, if it fails the validation, the tool will likely provide error messages indicating which aspects of the document are not compliant with the schema.

Method 2: Validating JSON file Using jsonlint Command

The jsonlint command is another effective tool for validating the JSON files in Linux environments. It can be used to check the syntax and structure of a JSON file and ensure it adheres to the JSON specification.

But before we use the jsonlint in Linux, first install the python3-demjson package in the system using the below-given command:

sudo apt install python3-demjson

To use jsonlint, you can simply type jsonlint followed by the name of the JSON file you want to validate. For example, if you have a JSON file called “data.json” in your current directory, you can run the following command to validate it:

jsonlint data.json

If the JSON file is valid, jsonlint will print ok.

However, if the file is not valid, it will print an error message indicating where the error occurred and what the problem is.

For example, if the file contains an error below output will appear.

Method 3: Validating JSON file Using jq Command in Linux

The jq command is third on the list to parse and validate JSON files. You need to install the jq package first on your Linux system from the following command:

sudo apt install jq

Then to validate the JSON file, use the following syntax:

jq.[file].json.

Example

jq . data.json

The command will print the JSON file indicating no error.

Now we will modify the JSON file and remove the inverted commas as shown below:

Now run the jq command by passing the name of JSON file:

jq . data.json

Below error will appear in the command line indicating the syntax error in the JSON file.

Conclusion

Validating JSON from the command line on Linux can be done using various tools such as JSON Spec, jq and jsonlint. These tools can parse and validate the JSON data, providing feedback on any errors or issues that need to be fixed. Using these tools, one can easily validate JSON from the command line on Linux and ensure the accuracy of the data.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.