JSON - Nesting children RABL or JBuilder for Rails JSON - Nesting children RABL or JBuilder for Rails json json

JSON - Nesting children RABL or JBuilder for Rails


The answer was to use a recursive function...

# encoding: UTF-8def json_ltree_builder( json, ltree_item )  json.title t( ltree_item.title )  json.attr do    json.id ltree_item.id  end  json.metadata do      json.val1 ltree_item.val1      json.val2 ltree_item.val2    end  children = ltree_item.children  unless children.empty?    json.children do      json.array! children do |child|        json_ltree_builder( json, child )      end    end  endendjson.array! @menu_items do |menu_item|  json_ltree_builder( json, menu_item )end

This builds something like

[  {  "title":"Title 1",     "attr" : {      "id": 111    },    "data" : {      "val1" : "Value 1",      "val2" : "Value 2"    },    "children" : [      {        "title":"Child 1",         "attr" : {          "id": 112        },        "data" : {          "val1" : "Value 1",          "val2" : "Value 2"        }      },      {        "title":"Child 2",         "attr" : {          "id": 112        },        "data" : {          "val1" : "Value 1",          "val2" : "Value 2"        }      }    ]  }]