CodeIgniter Forums
How to return pure json - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How to return pure json (/showthread.php?tid=78328)



How to return pure json - chanyui - 01-03-2021

Hello,

I want to respond with json without spaces or line breaks.
This is because the size of the large json grows unnecessarily.




PHP Code:
  public function testapi()
  {
    $data = ['test1' => 1'test2'=> 2];
    return $this->response->setJSON($data);
  


response

Code:
{
    "test1": 1,
    "test2": 2
}


Is there a good way?


RE: How to return pure json - includebeer - 01-03-2021

Have you tried with your environment set to production? It looks like it will not "pretty print" json in production. I don’t know if there’s a way to change this.
https://github.com/codeigniter4/CodeIgniter4/blob/v4.0.4/system/Format/JSONFormatter.php#L65


RE: How to return pure json - chanyui - 01-03-2021

When I put it in production mode, the problem was solved.
Thank you!