Skip to content

Instantly share code, notes, and snippets.

@jvmvik
Created February 25, 2016 18:09
Show Gist options
  • Save jvmvik/486aea32fbe030ffe058 to your computer and use it in GitHub Desktop.
Save jvmvik/486aea32fbe030ffe058 to your computer and use it in GitHub Desktop.
Ruby Render YAML JSON like format.
#!/bin/usr/env ruby
#
# Ruby script to render a YAML file with a nice format.
# Read a YAML file format and render like JSON.
#
# File: input.yml
# {
# key1: "value",
# key2: {
# key3: "value3"
# }
# }
#
# Output:
# {
# key1: "value",
# key2: {
# key3: "newValue"
# }
# }
#
# Dependencies Ruby 2+
require 'yaml'
require 'json'
# Load YAML file
yml = YAML.load(File.read("input.yml"))
# Render output as JSON (nicer display) -> YML
out = []
s = JSON.pretty_generate(yml).to_s
s.split("\n").each {|line| # Convert to YML
i = line.index(":")
if i
out << line[0..i].gsub('"','')
out << line[i+1..line.size] + "\n"
else
out << line + "\n"
end
}
# Display YML
puts out.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment