Validating JSON using Python jsonschema

August 27, 2013

JSON data format can be described using jsonschema, which can then be used to do validation of input JSON and all kinda of automated testing and processing of data. Coming from the Java and XML world, I find it very handy to validate incoming json requests on RESTful apis.

jsonschema is an implemenation of json-schema for Python, and its pretty easy to use.

Given your json data and an associated schema for the json that you have created, using the jsonschema library is pretty easy:

pip install jsonschema
import json
import jsonschema

schema = open("schema.json").read()
print schema

data = open("data.json").read()
print data

try:
    jsonschema.validate(json.loads(data), json.loads(schema))
except jsonschema.ValidationError as e:
    print e.message
except jsonschema.SchemaError as e:
    print e

This will validate your schema first and then validate the data. If you are sure your schema is valid, you can directly use one of the available Validators.

# Use a Draft3Validator
try:
    jsonschema.Draft3Validator(json.loads(schema)).validate(json.loads(data))
except jsonschema.ValidationError as e:
    print e.message

This will just report the first error it catches. Interestingly, you can also use lazy validation to report all validation errors:

# Lazily report all errors in the instance
try:
    v = jsonschema.Draft3Validator(json.loads(schema))
    for error in sorted(v.iter_errors(json.loads(data)), key=str):
        print(error.message)
except jsonschema.ValidationError as e:
    print e.message

For your reference, here is my sample json and its schema for you to start with. You can generate a basic schema out of your data using tools like http://www.jsonschema.net/. Or you can dive into http://json-schema.org/ yourself for details of the jsonschema spec.

data.json

{
  "address":{
    "streetAddress": "21 2nd Street",
    "city":"New York",
    "houseNumber":12
  },
  "phoneNumber":
    [
    {
      "type":"home",
      "number":"212 555-1234"
    }
  ]
}

schema.json

{
  "type":"object",
  "$schema": "http://json-schema.org/draft-03/schema",
  "required":false,
  "properties":{
    "address": {
      "type":"object",
      "required":true,
      "properties":{
        "city": {
          "type":"string",
          "required":true
        },
        "houseNumber": {
          "type":"number",
          "required":false
        },
        "streetAddress": {
          "type":"string",
          "required":true
        }
      }
    },
    "phoneNumber": {
      "type":"array",
      "required":false,
      "items":
      {
        "type":"object",
        "required":false,
        "properties":{
          "number": {
            "type":"string",
            "required":false
          },
          "type": {
            "type":"string",
            "required":false
          }
        }
      }
    }
  }
}
6 Comments on Validating JSON using Python jsonschema

Respond | Trackback

  1. Bkc says:

    Thanks for the post! I was struggling to figure out how to do the lazy validation part

  2. narendra says:

    Omg ur really awesome buddy.. was struggling to find a solution… kudos.. keep it up.. thanks a lot

  3. figgy says:

    Awesome style, Awesome content. Thank you very much!

  4. Robert says:

    Note that “required” form used in your examples (bool) is obsolete, new standard (implemented in the most majority of validators) treat it as an “array” of required node names.

    • Subhash Peshwa says:

      That’s true.. Please make a note of this. Still, nice and simple tutorial, just what I was looking for

  5. Hussain says:

    How to validate list of items?

Respond

Comments

Comments