Last active
January 4, 2024 17:52
-
-
Save dbrant/f260502106f9d3c9bae98cabfa29e8c2 to your computer and use it in GitHub Desktop.
Validate JSON from a URL against a Json Schema from another URL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Make sure to install 'jsonschema' as well as any validator packages, | |
# such as 'rfc3339-validator', that are used by the schema. | |
import json | |
import sys | |
import urllib.request | |
import urllib.error | |
import jsonschema | |
def main(): | |
if len(sys.argv) < 3: | |
print('Usage: validate_json.py <json_file_url> <json_schema_url>', file=sys.stderr) | |
sys.exit(1) | |
with urllib.request.urlopen(sys.argv[1]) as json_file: | |
json_data = json_file.read().decode() | |
with urllib.request.urlopen(sys.argv[2]) as json_schema_file: | |
json_schema = json_schema_file.read().decode() | |
jsonschema.validate(json.loads(json_data), json.loads(json_schema), format_checker=jsonschema.FormatChecker()) | |
print("Validation successful!") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment