From 65a1317cd1bb566aced7416354f3518b5632f8d3 Mon Sep 17 00:00:00 2001 From: aaronkaplan Date: Thu, 29 Apr 2021 13:02:24 +0200 Subject: [PATCH] Add python code to test the JSON schema. Works. Added testdata/ --- .gitignore | 2 ++ example_code/python/README.md | 5 +++ example_code/python/cofparser_jsonschema.py | 37 +++++++++++++++++++++ example_code/testdata/data.json | 2 ++ 4 files changed, 46 insertions(+) create mode 100644 .gitignore create mode 100644 example_code/python/cofparser_jsonschema.py create mode 100644 example_code/testdata/data.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..783cd5b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.swp + diff --git a/example_code/python/README.md b/example_code/python/README.md index 21c1c98..9c7490a 100644 --- a/example_code/python/README.md +++ b/example_code/python/README.md @@ -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. + + + diff --git a/example_code/python/cofparser_jsonschema.py b/example_code/python/cofparser_jsonschema.py new file mode 100644 index 0000000..02d6b7f --- /dev/null +++ b/example_code/python/cofparser_jsonschema.py @@ -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) diff --git a/example_code/testdata/data.json b/example_code/testdata/data.json new file mode 100644 index 0000000..b439f73 --- /dev/null +++ b/example_code/testdata/data.json @@ -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}