# JSON view not working

**URL:** <https://discourse.cakephp.org/t/json-view-not-working/12072>\
**Category:** Uncategorized\
**Created:** [June 18, 2024, 9:28am UTC](https://discourse.cakephp.org/t/json-view-not-working/12072 "2024-06-18T09:28:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yousuo](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yousuo](https://discourse.cakephp.org/u/yousuo)\
**Post date:** [June 18, 2024, 9:28am UTC](https://discourse.cakephp.org/t/json-view-not-working/12072/1 "2024-06-18T09:28:31Z")

</div>

I am trying to output json view (without the need of a template) in my Cake5 controller.

Base on [JSON Docs](https://book.cakephp.org/5/en/views/json-and-xml-views.html), my code is similar. but i am still getting the error ‘no template’.

Am I missing something ?

```auto
// My controller file. No template files has been created.

    public function initialize(): void {
        parent::initialize();

        $this->addViewClasses([JsonView::class,]);

    }

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

    public function testJsonView()
    {
        $articles = ['test view'];
        // Set the view vars that have to be serialized.
        $this->set(compact('articles', ));

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

```

 ![missing template](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/0/060cd6a59a15d99763b57e3fab1bdf5d8ed68aaf.png)

---

<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:** [June 18, 2024, 12:31pm UTC](https://discourse.cakephp.org/t/json-view-not-working/12072/2 "2024-06-18T12:31:05Z")

</div>

Your request needs to have set the `Accept` HTTP Header to `application/json`  
Otherwise Cake won’t render your request as such.

See [https://www.youtube.com/watch?v=GZPUlQEnAkA](https://www.youtube.com/watch?v=GZPUlQEnAkA)

---

<div class="post-metadata">

**Author:** ![yousuo](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yousuo](https://discourse.cakephp.org/u/yousuo)\
**Post date:** [June 18, 2024, 1:38pm UTC](https://discourse.cakephp.org/t/json-view-not-working/12072/3 "2024-06-18T13:38:23Z")

</div>

Thanks.

without specifying the extension `.json`, is there a way to ‘force’ it to JSON view in a GET request ?

In the previous version of Cake, this worked for me

```auto
        $this->RequestHandler->renderAs($this, 'json');
        $this->response->withType('application/json');
        $this->set('_serialize', true);

```

---

<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:** [June 18, 2024, 6:50pm UTC](https://discourse.cakephp.org/t/json-view-not-working/12072/4 "2024-06-18T18:50:47Z")

</div>

Just manually set the view class instead of relying on cake to do it for you.

```auto
$test = "test";
$this->set(compact('test'));
$this->viewBuilder()
    ->setOption('serialize', ['test'])
    ->setClassName('Json');

```

---

<div class="post-metadata">

**Author:** ![jmcd73](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jmcd73/32/481_2.png) [@jmcd73](https://discourse.cakephp.org/u/jmcd73)\
**Post date:** [June 20, 2024, 11:34pm UTC](https://discourse.cakephp.org/t/json-view-not-working/12072/5 "2024-06-20T23:34:22Z")

</div>

This allows you to choose a view by the client requesting it e.g. `http://yourhost/videos/export/json`

[https://book.cakephp.org/4/en/views/json-and-xml-views.html#choosing-a-view-class](https://book.cakephp.org/4/en/views/json-and-xml-views.html#choosing-a-view-class)
