Transform entity/array of entities to JSON


I have an array of entities (Doctrine objects) and I would like to transform it to JSON by leveraging Mautic loadApiMetada and serialization mechanism that is already provided by Mautic core so I can avoid manually composing the JSON.

Does anyone have any directions for me (has done this before) and knows what combination of services and method I need to use to get his?

Regards, M.


I am able to serialize the data using the follwoing line:


The thing is that I would like to exclude some fields as they are not relevant for my situation. And I know this is possible to do because api response returns response without those fields.

I solved it by using:

use Mautic\ApiBundle\Serializer\Exclusion\FieldExclusionStrategy;
$this->webhookModel->serializeData($records, null, [
            new FieldExclusionStrategy([
            ], 0)

I simply excluded the fields I did not want in JSON.