Created
February 25, 2016 18:09
-
-
Save jvmvik/486aea32fbe030ffe058 to your computer and use it in GitHub Desktop.
Ruby Render YAML JSON like format.
This file contains hidden or 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
| #!/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