We have a 3 level category structure set up for a site. I would like to output these categories as JSON, while keeping the structure intact. As far as I can see, EE's native categories only allow outputting category hierarchy as ul or ol. I also tried Child categories & GWcode categories plugins, but at maximum I was able to output the two top levels only. Anybody have any idea how to get this working?

1 Answer 1


Okay, I'll add this as a reference here, though I would avoid using it in a real life situation unless you are sure of what you are doing.

In stead of the many alternative ways to approach this, like using stash, embeds, a query or writing a plugin, I decided to tinker a little bit more with GWcode categories as it got me originally pretty close and seemed to have quite a few useful variables available.

While the solution could probably be easily modified to any arbitrary category structure depths, I highly recommend you do not use this solution in production. It is a messy hack (EE & JSON tend to get messy, but not this messy) and especially the last if-else structure feels a bit off, though it seemed to do what I needed it to:

    {exp:gwcode_categories group_id="1" style="linear"}
        {if depth1_start || depth2_start || depth3_start}

            "id": {cat_id},

        {if has_children}
            , "childcategories":[
        {if group_end}
            {if depth1_end && depth2_end && depth3_end}
            {if:elseif depth1_end && depth2_end}
            {if:elseif depth3_end}
]},{"performance": "{elapsed_time} seconds / {total_queries} queries"}]

An example of the JSON output can be found here: http://jsonlint.com/?json=https://dl.dropboxusercontent.com/u/381221/jsoncategories.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.