Skip to content

Instantly share code, notes, and snippets.

@lwoodson
Last active August 10, 2022 15:57
Show Gist options
  • Save lwoodson/a9421897b9ab85e395117b90269848da to your computer and use it in GitHub Desktop.
Save lwoodson/a9421897b9ab85e395117b90269848da to your computer and use it in GitHub Desktop.
Convert JSON to YAML via stdin (Ruby)
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o xtrace
sudo cat > /usr/local/bin/json2yaml <<EOT
#!/usr/bin/env ruby
require 'json'
require 'yaml'
input = ARGF.read()
json = JSON.load(input)
yaml = YAML.dump(json)
print yaml
EOT
sudo chmod +x /usr/local/bin/json2yaml
#!/usr/bin/env ruby
require 'json'
require 'yaml'
input = ARGF.read()
json = JSON.load(input)
yaml = YAML.dump(json)
print yaml
@lwoodson
Copy link
Author

lwoodson commented Nov 7, 2016

install

curl -sSL https://gist.githubusercontent.com/lwoodson/a9421897b9ab85e395117b90269848da/raw/1473b8088e5d90b9189f1fa4226cedebb8a5e818/install-json2yaml.bash | bash

use

cat template.json | json2yaml > template.yml

@lwoodson
Copy link
Author

lwoodson commented Nov 7, 2016

Probably easier ways, but #$%$ it.

@ericbn
Copy link

ericbn commented Aug 10, 2022

Or as a one-liner shell alias:

alias json2yml="ruby -rjson -ryaml -e 'print YAML.dump(JSON.load(ARGF.read()))'"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment