Answers.Body: <p>To get all root categories you can use: 'criteria' => ['level' => 1]</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-21T15:10:04.100Z
Answers.Text: thank you, and sorry for replying lately... but it works fine!
Answers.UserDisplayName: Yoshito Maeoka
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-26T12:34:32.740Z
Answers.LastActivityDate: 2017-04-26T12:34:32.740Z
Answers.UserDisplayName: Sean Tilley
Body: <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>
  
Tags: <elementcriteriamodel><plugin-elementapi>
Title: how can I filter to get only root categories in elementapi.php
ThreadText: # 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!  
  
  ---
  
  