<p>Have you tried grouping by category:</p>

<pre><code>'hostings.json' =&gt; [
        'elementType' =&gt; Entry::class,
        'criteria' =&gt; [
            'section'   =&gt; 'hostings',
            'groupBy' =&gt; 'hostingCompany[0].title'
        ],
        'paginate' =&gt; true,
        'transformer' =&gt; function(Entry $entry) {

            return [
                'hostingDomain'  =&gt; $entry-&gt;hostingDomain,
                'hostingCompany' =&gt; (string)$entry-&gt;hostingCompany[0],
                'url'            =&gt; $entry-&gt;url,
            ];
        },
    ],
</code></pre>

CC BY-SA 4.0
2019-08-23T13:22:50.563Z
2019-08-23T13:22:50.563Z
dmatthams
<p>This seems more like it may be an error in your code somewhere, do you have dev mode turned on?</p>

<p>I think you may want to output the title of the category:</p>

<pre><code>'hostingCompany' =&gt; (string)$entry-&gt;hostingCompany[0]-&gt;title,
</code></pre>

<p>See if that helps, if not I would update your question with your complete element-api.php</p>

CC BY-SA 4.0
2019-08-23T11:29:04.443Z
The output of the title is not the problem. It is displayed correctly. 
I just want to sort the JSON output by this field.
Steve
CC BY-SA 4.0
2019-08-23T11:32:08.507Z
You’re showing an error that suggests the json is incorrect, if you take your sorting code out are you saying the json outputs fine? (Albeit not in the order you want)
dmatthams
CC BY-SA 4.0
2019-08-23T11:34:58.617Z
Yes. If i delete the orderBy criteria all just works fine, except the sorting. If i open the JSON url in chrome i get the error above (updated question).
Steve
CC BY-SA 4.0
2019-08-23T13:23:45.653Z
Still seems like a syntax error where something is outputting a line break, surprised if it was that line but have posted another answer using 'groupBy'
dmatthams
CC BY-SA 4.0
2019-08-23T09:40:28.637Z
2019-08-23T09:40:28.637Z
dmatthams
<p>Is there a way to order the Craft Element-API results by the category title?</p>

<p>I'm just getting the error</p>

<pre><code>SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
</code></pre>

<p>The "hostingCompany" field is a category field.</p>

<pre><code>'hostings.json' =&gt; [
        'elementType' =&gt; Entry::class,
        'criteria' =&gt; [
            'section'   =&gt; 'hostings',
            'orderBy' =&gt; 'hostingCompany desc'
        ],
        'paginate' =&gt; true,
        'transformer' =&gt; function(Entry $entry) {

            return [
                'hostingDomain'  =&gt; $entry-&gt;hostingDomain,
                'hostingCompany' =&gt; (string)$entry-&gt;hostingCompany[0],
                'url'            =&gt; $entry-&gt;url,
            ];
        },
    ],
</code></pre>

<p>Updated error message:</p>

<pre><code>PHP Warning 'yii\base\ErrorException' with message 'Header may not contain more than a single header, new line detected' 
in /Applications/AMPPS/www/crm.local/vendor/yiisoft/yii2/web/Response.php:380
Stack trace:
0 /Applications/AMPPS/www/crm.local/vendor/craftcms/cms/src/web/ErrorHandler.php(81): yii\base\ErrorHandler-&gt;handleError(2, 'Header may not ...', '/Applications/A...', 380)
1 [internal function]: craft\web\ErrorHandler-&gt;handleError(2, 'Header may not ...', '/Applications/A...', 380, Array)
2 /Applications/AMPPS/www/crm.local/vendor/yiisoft/yii2/web/Response.php(380): header('HTTP/1.1 404 SQ...')
3 /Applications/AMPPS/www/crm.local/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response-&gt;sendHeaders()
4 /Applications/AMPPS/www/crm.local/vendor/yiisoft/yii2/base/Application.php(392): yii\web\Response-&gt;send()
5 /Applications/AMPPS/www/crm.local/web/index.php(21): yii\base\Application-&gt;run()
6 {main}
</code></pre>

<craft3><plugin-elementapi>
Element API - Order results by Category
# Element API - Order results by Category

(Asked by: username_0 on 2019-08-23)

Is there a way to order the Craft Element-API results by the category title?
I'm just getting the error
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

The "hostingCompany" field is a category field.
'hostings.json' => [
        'elementType' => Entry::class,
        'criteria' => [
            'section'   => 'hostings',
            'orderBy' => 'hostingCompany desc'
        ],
        'paginate' => true,
        'transformer' => function(Entry $entry) {

            return [
                'hostingDomain'  => $entry->hostingDomain,
                'hostingCompany' => (string)$entry->hostingCompany[0],
                'url'            => $entry->url,
            ];
        },
    ],

Updated error message:
PHP Warning 'yii\base\ErrorException' with message 'Header may not contain more than a single header, new line detected' 
in /Applications/AMPPS/www/crm.local/vendor/yiisoft/yii2/web/Response.php:380
Stack trace:
0 /Applications/AMPPS/www/crm.local/vendor/craftcms/cms/src/web/ErrorHandler.php(81): yii\base\ErrorHandler->handleError(2, 'Header may not ...', '/Applications/A...', 380)
1 [internal function]: craft\web\ErrorHandler->handleError(2, 'Header may not ...', '/Applications/A...', 380, Array)
2 /Applications/AMPPS/www/crm.local/vendor/yiisoft/yii2/web/Response.php(380): header('HTTP/1.1 404 SQ...')
3 /Applications/AMPPS/www/crm.local/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
4 /Applications/AMPPS/www/crm.local/vendor/yiisoft/yii2/base/Application.php(392): yii\web\Response->send()
5 /Applications/AMPPS/www/crm.local/web/index.php(21): yii\base\Application->run()
6 {main}

**Tags:** craft3, plugin-elementapi

---

## 2 answers

### Answer by username_1 on 2019-08-23. Score: 1

Have you tried grouping by category:
'hostings.json' => [
        'elementType' => Entry::class,
        'criteria' => [
            'section'   => 'hostings',
            'groupBy' => 'hostingCompany[0].title'
        ],
        'paginate' => true,
        'transformer' => function(Entry $entry) {

            return [
                'hostingDomain'  => $entry->hostingDomain,
                'hostingCompany' => (string)$entry->hostingCompany[0],
                'url'            => $entry->url,
            ];
        },
    ],


---

### Answer by username_1 on 2019-08-23. Score: 1

This seems more like it may be an error in your code somewhere, do you have dev mode turned on?
I think you may want to output the title of the category:
'hostingCompany' => (string)$entry->hostingCompany[0]->title,

See if that helps, if not I would update your question with your complete element-api.php

> **Comments:**
> **username_0**: The output of the title is not the problem. It is displayed correctly.  I just want to sort the JSON output by this field.  
> **username_1**: You’re showing an error that suggests the json is incorrect, if you take your sorting code out are you saying the json outputs fine? (Albeit not in the order you want)  
> **username_0**: Yes. If i delete the orderBy criteria all just works fine, except the sorting. If i open the JSON url in chrome i get the error above (updated question).  
> **username_1**: Still seems like a syntax error where something is outputting a line break, surprised if it was that line but have posted another answer using 'groupBy'  

---

