Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I recursively flatten a YAML file into a JSON object where keys are dot separated strings?

For example if I have YAML file with

en:
  questions:
    new: 'New Question'
    other:
      recent: 'Recent'
      old: 'Old'

This would end up as a json object like

{
  'questions.new': 'New Question',
  'questions.other.recent': 'Recent',
  'questions.other.old': 'Old'
}
like image 782
koonse Avatar asked Oct 22 '13 00:10

koonse


3 Answers

Since the question is about using YAML files for i18n on a Rails app, it's worth noting that the i18n gem provides a helper module I18n::Backend::Flatten that flattens translations exactly like this:

test.rb:

require 'yaml'
require 'json'
require 'i18n'

yaml = YAML.load <<YML
en:
  questions:
    new: 'New Question'
    other:
      recent: 'Recent'
      old: 'Old'
YML
include I18n::Backend::Flatten
puts JSON.pretty_generate flatten_translations(nil, yaml, nil, false)

Output:

$ ruby test.rb
{
  "en.questions.new": "New Question",
  "en.questions.other.recent": "Recent",
  "en.questions.other.old": "Old"
}
like image 160
wjordan Avatar answered Nov 12 '22 22:11

wjordan


require 'yaml'

yml = %Q{
en:
  questions:
    new: 'New Question'
    other:
      recent: 'Recent'
      old: 'Old'
}

yml = YAML.load(yml)
translations = {}

def process_hash(translations, current_key, hash)
  hash.each do |new_key, value|
    combined_key = [current_key, new_key].delete_if { |k| k.blank? }.join('.')
    if value.is_a?(Hash)
      process_hash(translations, combined_key, value)
    else
      translations[combined_key] = value
    end
  end
end

process_hash(translations, '', yml['en'])
p translations
like image 9
Ryan Bigg Avatar answered Nov 12 '22 22:11

Ryan Bigg


@Ryan's recursive answer is the way to go, I just made it a little more Rubyish:

yml = YAML.load(yml)['en']

def flatten_hash(my_hash, parent=[])
  my_hash.flat_map do |key, value|
    case value
      when Hash then flatten_hash( value, parent+[key] )
      else [(parent+[key]).join('.'), value]
    end
  end
end

p flatten_hash(yml) #=> ["questions.new", "New Question", "questions.other.recent", "Recent", "questions.other.old", "Old"]
p Hash[*flatten_hash(yml)] #=> {"questions.new"=>"New Question", "questions.other.recent"=>"Recent", "questions.other.old"=>"Old"}

Then to get it into json format you just need to require 'json' and call the to_json method on the hash.

like image 6
hirolau Avatar answered Nov 12 '22 23:11

hirolau