How to return json output from template file

I am very new to slim, and trying to develop a RESTful api for my existing php based web app. I want to return json output from a template file. How to do that? I am not getting any clue for it.

you wouldn’t render json from a template file.

$app->get('/data', function ($request, $response, $args) {
    $yourData = [
        'x' => 1
    ];
    return $response->withJson($yourData);
});