alkira/validate-json.py

44 lines
1.5 KiB
Python
Raw Normal View History

2022-06-15 10:43:29 +02:00
#!/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.
#
2022-06-17 15:13:16 +02:00
import sys
2022-06-15 10:43:29 +02:00
import json
2022-06-17 15:13:16 +02:00
import argparse
2022-06-15 10:43:29 +02:00
2022-06-17 15:13:16 +02:00
# Parse all arguments
2022-06-17 16:01:23 +02:00
parser = argparse.ArgumentParser(description="Validate JSON config")
2022-06-17 15:36:07 +02:00
parser.add_argument("-f", "--file", type=str, help="location of the JSON connector file")
2022-06-17 15:13:16 +02:00
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:
2022-06-15 10:43:29 +02:00
body = json.load(f)
2022-06-17 15:13:16 +02:00
if args.pretty:
print(json.dumps(body, indent=4))
else:
2022-06-17 15:36:07 +02:00
print(json.dumps(body))