<p>To get all root categories you can use: 'criteria' => ['level' => 1]</p>

CC BY-SA 3.0
2018-03-21T15:10:04.100Z
thank you, and sorry for replying lately... but it works fine!
Yoshito Maeoka
CC BY-SA 3.0
2017-04-26T12:34:32.740Z
2017-04-26T12:34:32.740Z
Sean Tilley
<p>I tried to build JSON-API which returns all categories, whose result will be cascaded like "nav" tag with craft twig templating (see: <a href="https://craftcms.com/docs/templating/craft.categories" rel="nofollow noreferrer">https://craftcms.com/docs/templating/craft.categories</a> ).</p>

<p>to get all root categories (which has no parent category, and which can have child categories), I've tried to filter giving within "criteria" something like that, but the result was just returned all categories:</p>

<pre><code>'endpoints' =&gt; [
    'api/categories' =&gt; [
        'elementType' =&gt; ElementType::Category,
        'criteria' =&gt; [ 'descendantOf' =&gt; null ],
        'transformer' =&gt; function($entry) {
            return [
                'id' =&gt; $entry-&gt;id,
                'title' =&gt; $entry-&gt;title,
                'parent' =&gt; ($entry-&gt;parent) ? $entry-&gt;parent-&gt;id : null,
                'children' =&gt; ($entry-&gt;children) ?
                    children_in_array($entry-&gt;children) : null,
            ];
        },
    ]
]
</code></pre>

<p><em>function <strong>children_in_array()</strong> returns same structure like result of 'transformer' function (id, title, parent, children).</em></p>

<p>How can I get only root categories in this way?</p>

<p>or is there other solution all cascaded category list in JSON format?</p>

<elementcriteriamodel><plugin-elementapi>
how can I filter to get only root categories in elementapi.php
# how can I filter to get only root categories in elementapi.php

(Asked by: username_0 on 2017-03-29)

I tried to build JSON-API which returns all categories, whose result will be cascaded like "nav" tag with craft twig templating (see: https://craftcms.com/docs/templating/craft.categories ).
to get all root categories (which has no parent category, and which can have child categories), I've tried to filter giving within "criteria" something like that, but the result was just returned all categories:
'endpoints' => [
    'api/categories' => [
        'elementType' => ElementType::Category,
        'criteria' => [ 'descendantOf' => null ],
        'transformer' => function($entry) {
            return [
                'id' => $entry->id,
                'title' => $entry->title,
                'parent' => ($entry->parent) ? $entry->parent->id : null,
                'children' => ($entry->children) ?
                    children_in_array($entry->children) : null,
            ];
        },
    ]
]

function children_in_array() returns same structure like result of 'transformer' function (id, title, parent, children).
How can I get only root categories in this way?
or is there other solution all cascaded category list in JSON format?

**Tags:** elementcriteriamodel, plugin-elementapi


---

## 1 answers

### Answer by username_1 on 2017-04-26. Score: 2 | Accepted answer

To get all root categories you can use: 'criteria' => ['level' => 1]

> **Comments:**
> **username_0**: thank you, and sorry for replying lately... but it works fine!  

---

