Add python code to test the JSON schema. Works.

Added testdata/
This commit is contained in:
aaronkaplan 2021-04-29 13:02:24 +02:00
parent ba0d3b01cd
commit 65a1317cd1
No known key found for this signature in database
GPG key ID: 1AED8B672DD4C9B1
4 changed files with 46 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.swp

View file

@ -3,4 +3,9 @@
This little package can parse the Passive DNS Common Output Format (COF) and validate it.
It is given as example code.
* cofparser.py ... a manually written parser for the COF format
* cofparser_jsonschema.py .... one which uses the JSON schema to validate.

View file

@ -0,0 +1,37 @@
import sys
import json
import jsonschema
from jsonschema import validate
def get_schema(filename):
"""This function loads the given schema available"""
with open(filename, 'r') as file:
schema = json.load(file)
return schema
def validate_json(json_data, schema=None):
"""REF: https://json-schema.org/ """
try:
validate(instance=json_data, schema=schema)
except jsonschema.exceptions.ValidationError as err:
print(err)
err = "Given JSON data is InValid"
return False, err
message = "Given JSON data is Valid"
return True, message
if __name__ == "__main__":
schema = get_schema("schema/schema.json")
# Convert json to python object.
with open(sys.argv[1], 'r') as ndjson_file:
for line in ndjson_file:
jsonData = json.loads(line)
# validate it
is_valid, msg = validate_json(jsonData, schema=schema)
print(msg)

2
example_code/testdata/data.json vendored Normal file
View file

@ -0,0 +1,2 @@
{"count":1909,"rdata":["cpa.circl.lu"],"rrname":"www.circl.lu","rrtype":"CNAME","time_first":1315586409,"time_last":1449566799}
{"count":2560,"rdata":["cpab.circl.lu"],"rrname":"www.circl.lu","rrtype":"CNAME","time_first":1449584660,"time_last":1617676151}