Skip to content

Instantly share code, notes, and snippets.

@danwaz
Created September 19, 2012 22:18
Show Gist options
  • Save danwaz/3752702 to your computer and use it in GitHub Desktop.
Save danwaz/3752702 to your computer and use it in GitHub Desktop.
Convert JSON to YAML
#!/bin/bash
# Converts a directory of JSON files into YAML files! Cool!
# Requires json2yaml node package https://npmjs.org/package/json2yaml
# receive the directory or treat current directory as the base directory
DIR=$1
if [ -d $DIR ]; then
cd $DIR;
echo $PWD;
# Loop through the directory looking for .JSON extensions
for f in *.json
do
FILE_NAME=`echo "$f" | grep -oP '.*(?=\.json$)'`
# only run this when we're sure there is no yml file for the $FILE_NAME
if [ ! -e "$FILE_NAME.yml" ]; then
# do the conversion
echo "Creating $FILE_NAME.yml"
json2yaml $f > "$FILE_NAME.yml"
else
echo "The yaml file for $f is already there, bro."
fi
done
# exit cleanly
exit 0
else
echo "No Such Directory, dood."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment