How to return JSON

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

Follow this section of the book: 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

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

1 Like

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);
3 Likes

Sorry, but I could not understand the documentation

Din0saur thanks, It worked.