JSON route not working

I have a route in my K2 site which I am trying to replicate in a K3 site. It almost works…

// Routes
'routes' => [
    'pattern' => 'uncss.json',
    'action'  => function () {
      header('Content-type: application/json; charset=utf-8');
        $pages = site()->index();
        $root = site()->find('home');
        $json = array();
        $json[] = (string)$root->url();

        foreach ($pages as $page) {
            $json[] = (string)$page->url();
        echo json_encode($json);

Except what im getting is the JSON echoed into the sites error page, its not returning as if it was a json file on the filesystem and i was accessing it directly. (this worked with K2).

How can I fix it?

well what do know… answered my own question seconds after posting…

return json_encode($json);