Convert a ruby nested hash in array of hashes -


i'm trying flat nested hash. hash has structure:

    {    "errordescription":"",    "message":"",    "resultcode":"ok",    "resultobj":{       "categorylist":[          {             "asnew":"n",             "categoryid":40000000,             "categorylist":[                {                   "asnew":"n",                   "categoryid":40000007,                   "categorylist":[                   ],                   "categoryname":"catalogo",                   "categorytype":"type_node",                   "contentid":40000007,                   "contenttitle":"catalogo",                   "contenttype":"category_node",                   "orderid":0,                   "urlpictures":""                },                {                   "asnew":"n",                   "categoryid":40000018,                   "categorylist":[                      {                         "asnew":"n",                         "categoryid":40000019,                         "categorylist":[                          ],                         "categoryname":"canali calcio",                         "categorytype":"type_vod",                         "contentid":40000019,                         "contenttitle":"calcio & sport",                         "contenttype":"category_leaf",                         "orderid":0,                         "urlpictures":""                      },                      {                         "asnew":"n",                         "categoryid":40000020,                         "categorylist":[                          ],                         "categoryname":"canali cinema",                         "categorytype":"type_vod",                         "contentid":40000020,                         "contenttitle":"cinema & serie",                         "contenttype":"category_leaf",                         "orderid":1,                         "urlpictures":""                      }                   ],                   "categoryname":"canali tv",                   "categorytype":"type_node",                   "contentid":40000018,                   "contenttitle":"canali tv",                   "contenttype":"category_node",                   "orderid":1,                   "urlpictures":""                }             ],             "categoryname":"root",             "categorytype":"type_node",             "contentid":40000000,             "contenttitle":"root",             "contenttype":"",             "orderid":0,             "urlpictures":""          }       ]    } } 

i must convert hash in array of objects, every object has structure:

                {                   "asnew":"n",                   "categoryid":40000007,                   "categoryname":"catalogo",                   "categorytype":"type_node",                   "contentid":40000007,                   "contenttitle":"catalogo",                   "contenttype":"category_node",                   "orderid":0,                   "urlpictures":""                } 

basically every key "categorylist" has array value , array has 1 or more hash key "categorylist". must move objects in every categorylist array array without categorylist 'key'.

edit

i add method hash class

class hash   def find_all_values_for(key)     result = []     result << self[key]     unless self.values.nil?       self.values.each |values|         values = [values] unless values.is_a? array         values.each |value|           result += value.find_all_values_for(key) if value.is_a? hash         end       end     end     result.flatten.compact   end end 

and map resulting array deleting categorylist key

h.map { |c| c.except!('categorylist') } 

it works meybe there more efficient way.

this code return requested array of objects.

def flatten_categorylist(categorylist)   categorylist.inject([]) |result, item|     result + flatten_categorylist(item.delete("categorylist")) << item   end end  flatten_categorylist(h["resultobj"]["categorylist"]) 

i unable run yours benchmark, judging solutions subjective.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -