mirror of
https://codeberg.org/hyperreal/bin
synced 2024-11-25 10:23:42 +01:00
Rewrite yaml2json in Python
This commit is contained in:
parent
fdad459398
commit
6257cfde0d
25
yaml2json
25
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")
|
||||
|
Loading…
Reference in New Issue
Block a user