Javatpoint Logo
Javatpoint Logo

JSON Schema Validator Python

JSON Schema in Python

JSON Schema is a Python language used to validate a JSON document. It contains key/value pairs, where each key is used to specify how specific JSON data should be structured. Providing understandable, high-quality data and documentation is possible using JSON Schema.

JSON Schema is a documentation of JSON that defines the Schema of any JSON data. The keys present in the JSON Schema have a different special meaning. In a JSON document, a schema specifies the data types for each field, much like a table definition in a SQL database does. It also specifies which fields are necessary and which are optional.

JSON Schema describes:

  • The format of the data which already exists.
  • A simple and understandable documentation
  • Structural validation used for automated testing and for validating client-submitted data.

The JSON Schema Validator available for Python is jsonschema.

Let's get into the depth of the jsonschema of Python.

Jsonschema

JSON Schema is implemented in Python via jsonschema. By creating a custom schema with jsonschema, we can validate any JSON document against it, and if it passes, we can say that the JSON document is valid.

There are some keywords in jsonschema in Python:

Keywords Meaning and Use
$schema It describes that the given schema is written with the draft v4 specification.
title It will give the title to the JSON schema.
description It will tell the description of the schema.
type It will describe the first constraint on the JSON data
properties It describes the values and keys of different aspects of the JSON file
maximum This specifies the maximum acceptable value and is the constraint that should be applied to the value.
minimum This specifies the minimum acceptable value and is the constraint that should be applied to the value.
pattern The string is deemed valid if the regular expression successfully matches the instance.
multipleOf If the outcome of dividing a numeric instance by the value of the keyword "multipleOf" is an integer, the instance is valid against this keyword.

These are some keywords used for the validation of JSON files in Python.

Validation of the JSON in Python

Firstly, we must check whether the string is valid JSON in Python.

When any JSON response is received from any API, we must validate it before performing any operation.

Python gives a module jsonschema for validating the JSON object against a schema. We can install this module directly from the command prompt or in any terminal using this command:

python -m pip install jsonschema

Now, let's validate the JSON object against the JSON schema.

The JSON package provides two methods to parse JSON data using Python.

  1. loads(): It parses the JSON from the string.
  2. load(): It parses JSON from a file.

If the string passed can't be decoded as JSON, it gives a ValueError. The JSON response can be validated through json.loads().

Let's see a simple example to check whether the JSON data is valid.

Code:

Output:

This JSON string is Valid:  False
This JSON string is Valid:  True

Explanation:

In this code, firstly, we have imported the jsonschema module. Then, we have made a function validate_JSON which checks whether the data given is valid. If the data passed is invalid, it will throw an exception; otherwise, it will validate the data. We have passed two different data to check if it is valid. The first data passed is invalid as it is not according to the schema, thus returning False. The second data is valid as it is in the proper schema syntax and, thus, returns True.

We can check the validation of the data by another method as follows:

Firstly, we must make a schema to check whether the data is valid.

Explanation:

We have made a schema in which we have defined objects and their types.

Now, we will add instances for checking the validation of the JSON.

Explanation:

We have declared an instance with different objects and their value. It will check the instance according to the schema. The instance is given values according to the schema; thus, it is valid.

We will check for a few more instances:

Output:

ValidationError: 10 is not of type 'string'

Failed validating 'type' in schema['properties']['Field']['items']:
    {'type': 'string'}

On instance['Field'][0]:
    10

Explanation:

In this instance, we have added the objects, but the object Field has numerical values but is set as a string value. Thus, it gives a validation error.

Explanation:

Though we have not set any value to the field object, it is valid data as an array can be empty.

How to validate JSON Objects using the Command line?

For validating the JSON objects using the command line, Python offers a module json.tool. Before performing any operation on a JSON object, like adding it to a file or sending it to the client, we must write the validated data into the file.

The below command is used to validate objects of the Python dictionary in a JSON formatted string.

Output:

{
    "Cust_ID": 110,
    "Product": "ABC"
}

Explanation:

In this, we have used json.tool module for validating the Python dictionary objects. This command can be run in the command line, and as the objects are valid and in JSON format, thus, it returns the objects as the output.

We can also validate the JSON file by creating a file in JSON format and validating it using json.tool.

1. Creating a JSON file

Let's create a JSON file with different Customer details.

We have added multiple customer ids, names, products, and prices in this JSON file and saved it as customer.json.

2. Validating the JSON file

To validate the JSON file, we need to run this command in the command prompt or terminal:

Output:

{
    "Cust_id": 1001,
    "Cust_name": "ABC",
    "Product": "Pen",
    "Price": 100,
    "Cust_id (2)": 1002,
    "Cust_name (2)": "XYX",
    "Product (2)": "Apple",
    "Price (2)": 300 
}

Explanation:

We have validated the JSON file as it is in the JSON format. It prints the objects of the file as an output.

We can validate multiple JSON files, files with multiple tuples, and files with nested objects. The Schema will be different for all the files, but the method to validate the file will be the same.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA