# How to return JSON

**URL:** <https://discourse.cakephp.org/t/how-to-return-json/745>\
**Category:** Need Help\
**Created:** [July 17, 2016, 11:18pm UTC](https://discourse.cakephp.org/t/how-to-return-json/745 "2016-07-17T23:18:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![juarezfrancojr](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@juarezfrancojr](https://discourse.cakephp.org/u/juarezfrancojr)\
**Post date:** [July 17, 2016, 11:18pm UTC](https://discourse.cakephp.org/t/how-to-return-json/745/1 "2016-07-17T23:18:48Z")

</div>

my code

```
public function teste() {
        $usuario = $this->Usuarios->newEntity();
        $usuario->first_name='Juarez';
        $this->set([
            '_serialize' => ['json'],
            'json' => $usuario,
        ]);
        $this->set('_serialize', true);
 }

```

but response contain layout (ex.: html, header, body, footer the cake debugger, etc), I just want string as a response.  
I tryed  
`$this->viewBuilder()->layout(null);`  
don’t work

---

<div class="post-metadata">

**Author:** ![adayth](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/adayth/32/171_2.png) [@adayth](https://discourse.cakephp.org/u/adayth)\
**Post date:** [July 18, 2016, 8:30pm UTC](https://discourse.cakephp.org/t/how-to-return-json/745/2 "2016-07-18T20:30:20Z")

</div>

Follow this section of the book: [http://book.cakephp.org/3.0/en/views/json-and-xml-views.html](http://book.cakephp.org/3.0/en/views/json-and-xml-views.html) and don’t forget to enable [http://book.cakephp.org/3.0/en/development/routing.html#file-extensions](http://book.cakephp.org/3.0/en/development/routing.html#file-extensions)

After that access your controller action with .json in the url. For example: $controller/teste.json

---

<div class="post-metadata">

**Author:** ![Din0saur](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/din0saur/32/323_2.png) [@Din0saur](https://discourse.cakephp.org/u/Din0saur)\
**Post date:** [July 21, 2016, 5:21pm UTC](https://discourse.cakephp.org/t/how-to-return-json/745/3 "2016-07-21T17:21:26Z")

</div>

I believe this is more like you want:

```
    $this->RequestHandler->respondAs('json');
    $this->response->type('application/json');  
    $this->autoRender = false; 
    echo json_encode($any_content);
```

---

<div class="post-metadata">

**Author:** ![juarezfrancojr](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@juarezfrancojr](https://discourse.cakephp.org/u/juarezfrancojr)\
**Post date:** [July 22, 2016, 8:00am UTC](https://discourse.cakephp.org/t/how-to-return-json/745/4 "2016-07-22T08:00:09Z")

</div>

Sorry, but I could not understand the documentation

---

<div class="post-metadata">

**Author:** ![juarezfrancojr](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@juarezfrancojr](https://discourse.cakephp.org/u/juarezfrancojr)\
**Post date:** [July 22, 2016, 8:00am UTC](https://discourse.cakephp.org/t/how-to-return-json/745/5 "2016-07-22T08:00:09Z")

</div>

Din0saur thanks, It worked.
