From 6257cfde0d1410b7899b34190735cdde5c8b29ed Mon Sep 17 00:00:00 2001 From: Jeffrey Serio <23226432+hyperreal64@users.noreply.github.com> Date: Tue, 9 Jul 2024 00:29:15 -0500 Subject: [PATCH] Rewrite yaml2json in Python --- yaml2json | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/yaml2json b/yaml2json index da02a4d..9aef73a 100755 --- a/yaml2json +++ b/yaml2json @@ -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")