How to check JSON format validation?

I guess using of jsonschema(https://pypi.python.org/pypi/jsonschema) can perform thot for you.


You might consider jsonschema to validate your JSON. Here is a program that validates your example. To extend this to your "20 keys", add the key names to the "required" list.

import jsonschema
import json

schema = {
    "type": "object",
    "properties": {
        "customer": {
            "type": "object",
            "required": ["lastName", "firstName", "age"]}},
    "required": ["service", "customer"]
}

json_document = '''{
    "service" : "Some Service Name",
    "customer" : {
        "lastName" : "Kim",
        "firstName" : "Bingbong",
        "age" : "99"
    }
}'''

try:
    # Read in the JSON document
    datum = json.loads(json_document)
    # And validate the result
    jsonschema.validate(datum, schema)
except jsonschema.exceptions.ValidationError as e:
    print("well-formed but invalid JSON:", e)
except json.decoder.JSONDecodeError as e:
    print("poorly-formed text, not JSON:", e)

Resources:

  • https://pypi.python.org/pypi/jsonschema
  • http://json-schema.org/example1.html

If your finding the json schema syntax confusing. Create your json as you want it and then run it though online-json-to-schema-converter and then use it in Rob's example above.

Tags:

Python

Json