Engineer in Tokyo

JSON Schema Validator 0.1a for Python

I just released the first version for a project that I've been working on since the Python Onsen. It's a validator for JSON Schema written in Python. It's based on the JSON Schema Proposal Second Draft.

The source can be downloaded here: jsonschema-0.1a.tar.gz
The source is on Bitbucket

JSON Schema's purpose is to allow validation of JSON documents much like XML Schema, DTD. You can use it to define what kind of data should be present in the document as well as the structure of the data. You might have some JSON for a contact like so:

{
  "name": "Ian Lewis",
  "email": "IanLewis@xyz.com",
  "address": "123 Main St.",
  "phone": "080-1942-9494"
}

And you could describe this in JSON Schema with the following:

{
  "type":"object",
  "properties":{
    "name": {"type":"string"},
    "age": {"type":"int", "optional":True},
    "email": {
      "type":"string",
      "pattern":"^[A-Za-z0-9][A-Za-z0-9\.]*@([A-Za-z0-9]+\.)+[A-Za-z0-9]+$"
    },
    "address": {"type":"string"},
    "phone": {"type":"string"}
  }
}

This can be validated with something like the following Python code:

</table></div>

It can be easily extended to include support for new properties or to override the default validation for standard properties so I think it could be used for a wide range of applications. I plan to use it for a Form Maker application (code) on GAE. Let me know what you think!

</p>
import jsonschema, simplejson

data = """{
</td></tr>
  "</span>name": "Ian Lewis",
</td></tr>
  "</span>email": "IanLewis@xyz.com",
</td></tr>
  "</span>address": "123 Main St.",
</td></tr>
  "</span>phone": "080-1942-9494"
</td></tr>
}"</span>""

schema = """{
</td></tr>
  "</span>type":"object",
</td></tr>
  "</span>properties":{
</td></tr>
    "</span>name": {"type":"string"},
</td></tr>
    "</span>age": {"type":"int", "optional":True},
</td></tr>
    "</span>email": {
</td></tr>
      "</span>type":"string",
</td></tr>
      "</span>pattern":"^[A-Za-z0-9][A-Za-z0-9\.]*@([A-Za-z0-9]+\.)+[A-Za-z0-9]+$"
</td></tr>
    },
    "</span>address": {"type":"string"},
</td></tr>
    "</span>phone": {"type":"string"}
</td></tr>
  }
}"</span>""

x = simplejson.loads(data)
s = simplesjson.loads(schema)
jsonschema.validate(x,s)