JSON to Hash, vertical parsing

parsing large blocks of JSON is a pain in ass, especially traversing a large JSON up or down on a single level (non-nested)

{ 'expand': [

'item' etc etc

'item' etc etc

solution is to create a of whatever vertical key, values you need, insert each Hash instance into an Array, and then use a Position index to traverse the array up or down

require 'json'

jsonfile = File.read(ARGV[0])

data = JSON.parse(jsonfile)

h_array = []

n=0 # counter for array hash position

data['changelog']['histories'].each do |history|

history['items'].each do |item|

# convert JSON to Hash for vertical/sequential processing

hash = {

'position' => n,

'toString' => item['toString'],

'to' => item['to'],

'fromString' => item['fromString'],

'from' => item['from']

}

# add hash value to array

h_array.push(hash)

n = n + 1

end

end

# print last array element

puts h_array[-1]