Response in middleware don't have withJson method

How can set response to json in middleware
as I do in closure with
return $response->withJson([‘one’=>1,‘two’=>2]);

The withJson method has been gone in Slim 4, because it’s not part of the PSR-7 http interface.

Instead you have a lot of other possibilities:

  1. Create the json response manually
$response->getBody()->write((string)json_encode(['one' => 1, 'two' => 2]));

$response = $response->withHeader('Content-Type', 'application/json');

return $response;

If you need this very often, I would recommend using a JsonRenderer. Example Action