Class: JSONTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/transform/flatten.rb,
lib/transform/nothing.rb

Instance Method Summary collapse

Instance Method Details

#flatten(json, prefix) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/transform/flatten.rb', line 6

def flatten(json, prefix)
  json.keys.each do |key|
    if prefix.empty?
      full_path = key
    else
      full_path = [prefix, key].join('.')
    end

    if json[key].is_a?(Hash)
      value = json[key]
      json.delete key
      json.merge! flatten(value, full_path)
    else
      value = json[key]
      json.delete key
      json[full_path] = value
    end
  end
  return json
end

#transform(json) ⇒ Object



2
3
4
# File 'lib/transform/flatten.rb', line 2

def transform(json)
  return flatten(json, "")
end