alkira/validate-json.py
2022-06-17 16:01:23 +02:00

45 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Copyright 2022, Mischa Peters <mischa AT alkira DOT net>, Alkira.
# validate-json.py
# Version 0.1 - 20220617 - initial release
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
import sys
import json
import argparse
# Parse all arguments
parser = argparse.ArgumentParser(description="Validate JSON config")
parser.add_argument("-f", "--file", type=str, help="location of the JSON connector file")
parser.add_argument("-p", "--pretty", help="make the JSON pretty!", action="store_true")
if len(sys.argv)==1:
parser.print_help(sys.stderr)
sys.exit(1)
try:
args = parser.parse_args()
json_file = args.file
except argparse.ArgumentError as e:
print(str(e))
sys.exit()
with open (json_file, 'r') as f:
body = json.load(f)
if args.pretty:
print(json.dumps(body, indent=4))
else:
print(json.dumps(body))