Welcome Guest, Not a member yet? Register   Sign In
There is anyway to ignore null value from json response?
#5

(09-26-2021, 03:26 AM)InsiteFX Wrote: No sample code, but you can look at filters in the CodeIgniter 4 User Guide. Also take a look at the HTTP responses.

Thanks for the tip, I have solved that by below filter:

Register in $global 'after' App/Filters.php
Code:
<?php

namespace App\Filters;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

class JsonFilter implements FilterInterface
{
    function __construct()
    {
    }

    public function before(RequestInterface $request, $arguments = null)
    {
    }

    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
    {
        $json = $response->getJSON();
        $body = preg_replace('/,\s*"[^"]+": null|"[^"]+": null,?/', '', $json);
        $response->setJSON($body);
    }
}
Reply


Messages In This Thread
RE: There is anyway to ignore null value from json response? - by 2k10cs86 - 09-26-2021, 04:52 AM



Theme © iAndrew 2016 - Forum software by © MyBB