Symfony JSON response turns array of objects with only one object into an object

My JSON response is supposed to return an array of objects. However, if my array of objects only contains one object, JSON turns it into an object instead. Is there a way to always return an array of objects, even if it only contains one object? I need to get the length of my array but this won’t work because my response sometimes returns an object which causes .length not to work.

$response = JsonResponse::fromJsonString(
                    'groups' => $apiContext->groups,
                    'hypermedia' => $apiContext->hypermedia

$e is a ViewEvent. My controller returns an object which contains passengers like this:

-passengers: array:5 [
    "id" => "c90e76ad-43e1-48bc-b72c-07f9cf9942d8"
    "username" => "x"
    "roles" => array:1 [
      0 => "ROLE_USER"
    "password" => null
    "salt" => null

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *