Created
June 3, 2016 13:37
-
-
Save otobrglez/66274639697f377de8ec8047a248b8f0 to your computer and use it in GitHub Desktop.
YAML to JSON - one-liner with Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Single line of Ruby <3. | |
ruby -rjson -ryaml -e "puts YAML.load_file('my_file.yml').to_json" | |
# You can also pipe it to Python to get pretty ouput | |
ruby -rjson -ryaml -e "puts YAML.load_file('my_file.yml').to_json" | \ | |
python -mjson.tool | |
# Thats all. :) |
I'd like to load all yml from a relative path to json:
possibly : ruby -rjson -ryaml -e "puts YAML.load_file(File.dir(FILE).(all_yaml_files_in_directory.yaml').to_json"
This works, however, unlike the python -m json.tool
, the Ruby version doesn't sort the JSON. I'm doing this from inside vim too. So this is how I'm doing it to get it sorted, then re-spaced to 2 spaces.
:%!python -m json.tool
:%!ruby -rjson -e "puts JSON.pretty_generate(JSON.parse(ARGF.read))" %
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or if you want pretty-printed JSON without python: