Rewrite yaml2json in Python

This commit is contained in:
Jeffrey Serio 2024-07-09 00:29:15 -05:00
parent fdad459398
commit 6257cfde0d

View File

@ -1,17 +1,20 @@
#!/usr/bin/env bash
#!/usr/bin/env python3
#
# YAML to JSON conversion script
# Based on https://www.geeksforgeeks.org/convert-yaml-to-json/
#
# This script takes a YAML file as the first arg and outputs the
# converted JSON to stdout.
# This script takes a YAML file as the first arg, converts the
# YAML content to JSON, and outputs the converted JSON content
# to stdout.
set -e
import json
import sys
if ! python -c "import yaml" &>/dev/null; then
echo -e "PyYAML is not installed."
fi
import yaml
if ! python -c "import yaml, json; print(json.dumps(yaml.load(open('${1}'), Loader=yaml.FullLoader), indent=4))"; then
echo "Some kinda error occurred while attempting to convert YAML to JSON"
fi
try:
print(json.dumps(yaml.load(open(sys.argv[1]), Loader=yaml.FullLoader), indent=4))
except IndexError:
print("YAML file must be supplied as first arg")
except FileNotFoundError:
print("YAML file not found")