PYnative

Python Programming

  • Learn Python
  • Exercises
  • Quizzes
  • Code Editor
  • Tricks
Home » Python » JSON » Validate JSON data using Python

Validate JSON data using Python

Updated on: May 14, 2021 | 6 Comments

In this article, we will see how to validate JSON data using Python. There are multiple scenarios where we need different types of JSON validation. In this article, we will cover the followings

  • Check if a string is valid JSON in Python: Here we can check if a string is valid JSON before parsing it. For example, if you are working with any API, what if it returns Invalid JSON or any other data? Here, we will validate JSON as per the standard convention format.
  • Validate JSON Schema using Python: Here we will see how to validates incoming JSON data by checking if there all necessary fields present in JSON and also validate data types of those fields.

Further Reading:

  • Solve Python JSON Exercise to practice Python JSON skills
Explained how to validate JSON in Python
Explained how to validate JSON in Python

Check if a string is valid JSON in Python

When we receive the JSON response from any API, we must validate it before performing any operation using that data. There are various ways to validate JSON as per the standard convention format.

Using json.loads and json.load() method:

As we know, the json module provides two methods to parse JSON data using Python.

  • json.loads(): To parse JSON from String.
  • json.load() to Parse JSON from a file.

Both methods will throw a ValueError if the string or data you pass can’t be decoded as JSON. When we receive a JSON response, we can pass it to the json.loads() method to validate it as per the standard convention. Let’s see the example.

import json

def validateJSON(jsonData):
    try:
        json.loads(jsonData)
    except ValueError as err:
        return False
    return True

InvalidJsonData = """{"name": "jane doe", "salary": 9000, "email": "jane.doe@pynative.com",}"""
isValid = validateJSON(InvalidJsonData)

print("Given JSON string is Valid", isValid)

validJsonData = """{"name": "jane doe", "salary": 9000, "email": "jane.doe@pynative.com"}"""
isValid = validateJSON(validJsonData)

print("Given JSON string is Valid", isValid)

Output:

Given JSON string is Valid False
Given JSON string is Valid True
  • As you can see in the first example, we passed an invalid JSON string to the load method. In the first JSON data, we have added an extra comma to make it invalid because of this json.loads method generated a valueError.
  • In the second call, we passed a valid JSON document, and it successfully parsed by the json.loads method.

Note: Use json.load() method instead of json.loads() to parse and validate JSON from a file.

Validate JSON Object from the command line before writing it in a file

Python provides The json.tool module to validate JSON objects from the command line. When we send JSON response to a client or when we write JSON data to file we need to make sure that we write validated data into a file

Run a below command on the command line. Here we are validating the Python dictionary in a JSON formatted string.

echo {"id": 1, "item": "itemXyz"} | python -m json.tool

Output:

{
    "id": 1,
    "name": "itemXyz"
}

Let’s pass an invalid object for JSON validating.

echo {"id": 1 "name": "Jessa Duggar"} | python -m json.tool

Output:

Expecting ',' delimiter: line 1 column 10 (char 9)

We can also use different command-line options of json.tool module to validate JSON. Let’s see those

Validate JSON File

Let’s assume that you want to parse a JSON file using Python. Instead of directly parsing it, we need to validate it so that we can assure that file content is a valid JSON document. Let’s see how to use a command-line option of a json.tool module to validate file containing JSON data.

File content before running the following command

JSON file to be validated
JSON file to be validated

Command:

python -m json.tool fileName

Example:

python -m json.tool studentDetails.json

We received the following error because the file content is not in JSON format.

error expecting property name enclosed in double quotes: line 1 column 2 (char 1)

Executed the same command after correcting an error

Output:

{
    "id": 1,
    "name": "Jessa Duggar",
    "class": 9,
    "attendance": 75,
    "subjects": [
        "English",
        "Geometry",
        "Physics",
        "World History"
    ],
    "email": "jess@example.com"
}

Validate JSON Schema using Python

Sometimes we need something extra than just a standard JSON validation. i.e., We will see how to validate incoming JSON data by checking all necessary fields present in JSON file or response and also validate data types of those fields.

Such a scenario includes the following things:

  • We need the necessary fields present in JSON file
  • We need data of a JSON filed in a type that we want. For example, we want all numeric fields in the number format instead of number encoded in a string format like this Marks: "75" so we can use it directly instead of checking and converting it every time.

We need to use the jsonschema library. This library is useful for validating JSON data. The library uses the format to make validations based on the given schema. jsonschema is an implementation of JSON Schema for Python.

Using jsonschema, we can create a schema of our choice, so every time we can validate the JSON document against this schema, if it passed, we could say that the JSON document is valid.

Follow the below steps:

  • First, install jsonschema using pip command. pip install jsonschema
  • Define Schema: Describe what kind of JSON you expect
  • Convert JSON to Python Object using json.load or json.loads methods.
  • Pass resultant JSON to validate() method of a jsonschema. This method will raise an exception if given json is not what is described in the schema.

Let’s see the example. In this example, I am validating student JSON. The following conditions must meet to call it as a valid JSON

  • The student name and roll number must be present in JSON data.
  • Marks and roll number must be in a number format.
import json
import jsonschema
from jsonschema import validate

# Describe what kind of json you expect.
studentSchema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "rollnumber": {"type": "number"},
        "marks": {"type": "number"},
    },
}

def validateJson(jsonData):
    try:
        validate(instance=jsonData, schema=studentSchema)
    except jsonschema.exceptions.ValidationError as err:
        return False
    return True

# Convert json to python object.
jsonData = json.loads('{"name": "jane doe", "rollnumber": "25", "marks": 72}')
# validate it
isValid = validateJson(jsonData)
if isValid:
    print(jsonData)
    print("Given JSON data is Valid")
else:
    print(jsonData)
    print("Given JSON data is InValid")

# Convert json to python object.
jsonData = json.loads('{"name": "jane doe", "rollnumber": 25, "marks": 72}')
# validate it
isValid = validateJson(jsonData)
if isValid:
    print(jsonData)
    print("Given JSON data is Valid")
else:
    print(jsonData)
    print("Given JSON data is InValid")

Output:

{'name': 'jane doe', 'rollnumber': '25', 'marks': 72}
Given JSON data is InValid
{'name': 'jane doe', 'rollnumber': 25, 'marks': 72}
Given JSON data is Valid

Note: The validate() method will raise an exception if given JSON is not what is described in the schema.

The first JSON data contains roll number value in string format instead of a number so when we called validate() method it returned False. If print exception it will show like this.

Failed validating 'type' in schema['properties']['rollnumber']:
    {'type': 'number'}

On instance['rollnumber']:
    '25'

Filed Under: Python, Python JSON

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!

Related Tutorial Topics:

Python Python JSON

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Posted In

Python Python JSON
TweetF  sharein  shareP  Pin

  Python JSON

  • Python JSON Guide
  • JSON Serialization
  • JSON Parsing
  • JSON Validation
  • PrettyPrint JSON
  • Make Python Class JSON serializable
  • Convert JSON Into Custom Python Object
  • Python JSON Unicode
  • Parse JSON using Python requests
  • Post JSON using Python requests
  • Serialize DateTime into JSON
  • Serialize Python Set into JSON
  • Serialize NumPy array into JSON
  • Parse multiple JSON objects from file
  • Parse Nested JSON
  • Python JSON Exercise

All Python Topics

Python Basics Python Exercises Python Quizzes Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our Terms Of Use, Cookie Policy, and Privacy Policy.

Copyright © 2018–2023 pynative.com