Skip to content

Instantly share code, notes, and snippets.

@xhan
Created April 5, 2013 10:30
Show Gist options
  • Save xhan/5318278 to your computer and use it in GitHub Desktop.
Save xhan/5318278 to your computer and use it in GitHub Desktop.
simple script to convert between JSON and YAML
#! /usr/bin/env ruby
def usage
puts "USAGE: binary file.json/yaml [json/yaml] "
true
end
require 'yaml'
require 'json'
TYPE_JSON, TYPE_YAML = ['json','yaml']
def main
file , type = ARGV
content = nil
usage and return unless file
# get type by file extension if not defined
type = file.split('.').last unless type
type.downcase! if type
unless type or [TYPE_JSON,TYPE_YAML ].include? type.downcase
usage and return
end
case type
when TYPE_YAML
content = YAML::load( File.open(file) ) #.to_json
content = JSON.pretty_generate( content )
when TYPE_JSON
content = JSON::load( File.open(file) ).to_yaml
end
puts content
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment