# JSON view example from docs gives "Missing Template error"

**URL:** <https://discourse.cakephp.org/t/json-view-example-from-docs-gives-missing-template-error/10536>\
**Category:** Need Help\
**Created:** [July 23, 2022, 12:57am UTC](https://discourse.cakephp.org/t/json-view-example-from-docs-gives-missing-template-error/10536 "2022-07-23T00:57:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jubbs](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@Jubbs](https://discourse.cakephp.org/u/Jubbs)\
**Post date:** [July 23, 2022, 12:57am UTC](https://discourse.cakephp.org/t/json-view-example-from-docs-gives-missing-template-error/10536/1 "2022-07-23T00:57:32Z")

</div>

I am trying to output json. using the example in the docs here [https://book.cakephp.org/4/en/views/json-and-xml-views.html](https://book.cakephp.org/4/en/views/json-and-xml-views.html)

Going to the following URL gives me a missing template error. I just want the json I dont want to use a template

[http://localhost:8765/Articles/index](http://localhost:8765/Articles/index)

What am I doing wrong?

```auto

namespace App\Controller;

use Cake\View\JsonView;

class ArticlesController extends AppController
{
    public function viewClasses(): array
    {
        return [JsonView::class];
    }

    public function index()
    {
        // Some code that created $articles and $comments
        $articles = [
            "one" => "Article 1",
            "two" => "Article 2",
            "three" => "Article 3",
            "four" => "Article 4",

        ];
        // Set the view vars that have to be serialized.
        $this->set("articles",$articles);

        // Specify which view vars JsonView should serialize.
        $this->viewBuilder()->setOption('serialize', 'articles');
    }
}

```

---

<div class="post-metadata">

**Author:** ![Jubbs](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@Jubbs](https://discourse.cakephp.org/u/Jubbs)\
**Post date:** [July 23, 2022, 1:34am UTC](https://discourse.cakephp.org/t/json-view-example-from-docs-gives-missing-template-error/10536/2 "2022-07-23T01:34:53Z")

</div>

I found the issue missing a line in the Docs

```auto
$this->viewBuilder()->setClassName("Json");
```

---

<div class="post-metadata">

**Author:** ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)\
**Post date:** [July 29, 2022, 12:41pm UTC](https://discourse.cakephp.org/t/json-view-example-from-docs-gives-missing-template-error/10536/3 "2022-07-29T12:41:30Z")

</div>

in 3.1 when you don’t want template  
`$this->autoRender = false;`  
helps

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [July 29, 2022, 5:20pm UTC](https://discourse.cakephp.org/t/json-view-example-from-docs-gives-missing-template-error/10536/4 "2022-07-29T17:20:26Z")

</div>

@Jubbs You should know that the way how content type negotiation works is not only decided by the server but also by the client.

So in the example of the docs you are only adding the possibility of a JSON content type to your controller actions via

```auto
public function viewClasses(): array
{
    return [JsonView::class];
}

```

and

```auto
$this->viewBuilder()->setOption('serialize', 'articles');

```

inside your action.

But this doesn’t mean it is **forced** to only be JSON, it can still be a simple HTTP request with the default template which returns HTML.

If you want JSON as a response your client should specify it wants JSON via the `Accept` Header which should be set to `application/json`

It of course depends on what client you are using, in e.g. jQuery it is the option `dataType: 'json',`  
In Postman you can set additional headers easily like so

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/a/a7e4427d528df42d258c487d3846a68cab67aef1.png)  
and many many more possibilites of course.

You can alternativly add

```auto
$routes->setExtensions(['json']);

```

to your `config/routes.php` (or plugin routes) to allow `JSON` **routes**.  
This means, that you can e.g. call your action via

```auto
http://localhost/mycontroller/myaction/3.json

```

instead of

```auto
http://localhost/mycontroller/myaction/3

```

and get a direct JSON response without having the need to manually set the JSON View Class via

```auto
$this->viewBuilder()->setClassName("Json");

```

---

<div class="post-metadata">

**Author:** ![midnightcheese](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/midnightcheese/32/2550_2.png) [@midnightcheese](https://discourse.cakephp.org/u/midnightcheese)\
**Post date:** [August 19, 2022, 2:07am UTC](https://discourse.cakephp.org/t/json-view-example-from-docs-gives-missing-template-error/10536/5 "2022-08-19T02:07:56Z")

</div>

Thanks, @Jubbs ! This was tripping me up as well.
