12

In Laravel 5.5

Route::get('/test2', function (){
    $data = (object) [];

    return response()->json($data);

});

it always returns [] rather than {}.


another code:

Route::get('/test2', function (){
    $data = (object) [];

    return json_encode($data);

});

it correctly returns {}


I want to use response()->json() helper to return empty object instead of empty array, is it possible?

3
  • Possible duplicate of How to define an empty object in PHP Commented May 1, 2018 at 8:27
  • @DrNio not the same problem. see my code, already defined a object data, but problem is using response() -> json() always return empty array for the data. Commented May 1, 2018 at 8:42
  • 1
    Hmm your first variant should be ok as well since by default it uses the json_encode. Unless your ResponseFactory does not return the default JsonResponse. Could you try this: return response()->json($data, 200, [], JSON_FORCE_OBJECT); Commented May 1, 2018 at 8:44

6 Answers 6

15

This works:

return response()->json(new stdClass());
Sign up to request clarification or add additional context in comments.

Comments

7

One more example, inspired by Hamid Mohayeji answer:

return \response()->json(null);

No need to instantiate stdClass (memory allocation though).

Comments

6

This works in Laravel 5.6

Route::get('/test2', function (){
    $data = (object) [];

    return response()->json($data, 200, [], JSON_FORCE_OBJECT);

});

2 Comments

This works in Laravel 9 too, and should be the accepted answer. Using the facade: Response::json([], options: JSON_FORCE_OBJECT);
This not good solution, because if your $data has deep nested array of litters it also be forced to keyed object style syntax: $data = ['names' => ['John', 'Doe']] produces such json {"names": { "0":"John", "1":"Doe" }} Better approach is to use if statement: // returns '{}' json for empty array if (empty($data)) return response()->json(null);
2

Using dingo/api modifies the answer of the empty array as @kingshark said, but you can make this work without stoping using dingo or modifying anything else than your own response.

Dingo expects the $data to be already encoded as json, so, if you do:

$data = ['message' => 'Lorem Ipsum', 
         'errors' => []];
return response()->json($data);

Will get:

{
    "message": "Lorem Ipsum",
    "errors": []
}

But if you do:

$data = ['message' => 'Lorem Ipsum', 
         'errors' => []];
$data = json_encode($data, JSON_FORCE_OBJECT);
return response()->json($data);

You will get:

{
    "message": "Lorem Ipsum",
    "errors": {}
}

1 Comment

I'm getting "{}" instead of {} only. Any idea?
1

When you return response()->json($data), it returns a JsonResponse object which includes $data in data field. So when you receive this response, you get the data with the same format(object).

JsonResponse json(string|array $data = [], int $status = 200, array $headers = [], int $options)

When you return json_encode($data), it will parse the $data and returns a string: "{}". Only if you decode "{}", it will become object again.

string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )

Comments

-1

Thanks everyone, finally I found the problem: dingo/api.

If I use response()->json($data) in app using dingo/api package, there is a different response handling process. At some point, it will go through \Dingo\Api\Http\Response::makeFromJson method which decode the response content, then create a new response instance, which changed {} to [].

If I removed or do not use dingo/api package, response()->json() can work well, at least in Laravel 5.5.

dingo/api override, change and extend large amount of laravel built-in classes and approach in the entire request life cycle, from route to response to exception handling.

1 Comment

This does not work, atleast in Laravel 8. Result is still empty array, not object. Solution that works is empty stdClass like Tarasovych answered above.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.