Pretty Printing YAML with the Ruby Command-Line

Today I had a pretty fun little thing to do - validate that a YAML file was well formed (because it turns out, it wasn't).

I wanted a handy one-liner to confirm it, similar to my Pretty Print JSON series.

As I use Ruby for my primarily scripting language of choice, this example will be done with Ruby.

To parse the YAML file, and then pretty-print it, we can simply run:

ruby -ryaml -e 'puts YAML.load(ARGF.read).to_yaml' file.yaml
ruby -ryaml -e 'puts YAML.load(ARGF.read).to_yaml' < file.yaml

And because YAML is a valid superset of JSON, we can also use the same for JSON files!

ruby -ryaml -e 'puts YAML.load(ARGF.read).to_yaml' file.json
ruby -ryaml -e 'puts YAML.load(ARGF.read).to_yaml' < file.json

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#nablopomo #blogumentation #ruby #yaml #pretty-print #command-line.

This post was filed under articles.

Has this content helped you? Please consider supporting me so I can continue to create content like this!

This post is part of the series pretty-print-yaml.

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.