abhishek009's avatar

JSONarray to JSONobject

Hell there,

By default laravel returns JSONArray but I want JSONObject. I tried to use toJson() but it doesn't work.

Any help ?

0 likes
12 replies
phildawson's avatar

Well if you want everything including non-associative arrays like on a Collection or empty to return {} then this:

->toJson(JSON_FORCE_OBJECT);
abhishek009's avatar

Well I don't want starting "[" and ending "]" that means a JSONObject :)

abhishek009's avatar
[{"id":4,"nicks":"abhishek","user_id":6,"created_at":"2015-06-04 23:33:59","updated_at":"2015-06-02 23:33:59"}]

How do I use it ? @phildawson

phildawson's avatar

@abhishek009 Well a single result should be being represented as an object, have you got the code that is used to get that output?

As a basic test if that is from a Foo model

Route::get('test', function(App\Foo $model){
    return $model->find(4);
});

would output

{"id":4,"nicks":"abhishek","user_id":6,"created_at":"2015-06-04 23:33:59","updated_at":"2015-06-02 23:33:59"}

If you copy that snippet and replace App\Foo with your model what do you get when you visit /test

abhishek009's avatar

@phildawson

$query =Nicks::all();
return $query;

In controller I have done this. But still it shows the same. :(

[
{
"id": 4,
"name": "abhishek",
"user_id": 6,
"created_at": "2015-06-04 23:33:59",
"updated_at": "2015-06-02 23:33:59"
},
    
{
"id": 12,
"name": "opo",
"user_id": 6,
"created_at": "2015-06-12 17:01:03",
"updated_at": "2015-06-12 19:48:06"
}
]
bobbybouwmann's avatar

You return a collection, so obviously you get an json array back... You should return one object instead of a collection

$query = Nicks::all(); // This returns a collection
$query = Nicks::first(); // This returns a single object

return $query;
phildawson's avatar

@abhishek009 Just like above and you would also need to explicitly set the application/json header in this case.

$query =Nicks::all();
$jsonObject = $query->toJson(JSON_FORCE_OBJECT);

return response($jsonObject, 200, ["Content-Type" => "application/json"]);

An array of results like a Collection from all() would then do this kind of thing with the index as key.

{
    "0" : {
        "id": 4,
        "name": "abhishek",
        "user_id": 6,
        "created_at": "2015-06-04 23:33:59",
        "updated_at": "2015-06-02 23:33:59"
    },
    "1" : {
        "id": 12,
        "name": "opo",
        "user_id": 6,
        "created_at": "2015-06-12 17:01:03",
        "updated_at": "2015-06-12 19:48:06"
    }
}
phildawson's avatar
Level 26

@absiddiqueLive The Model/Collections will use toJson() by default when __toString() is tried so no need to do that.

    public function __toString()
    {
        return $this->toJson();
    }

So this is the same.

return User::with('roles')->first();
return User::find(1);
return User::all();

This also doesn't make sense to do.

return Response::json(User::all()->toArray());

as when you try and use a Collection as a string it does toArray behind the scenes for you.

    public function toJson($options = 0)
    {
        return json_encode($this->toArray(), $options);
    }

so if he wanted a json array he would just do return User::all(); like above

he's also wanting the array as an object though, and would also mean the Response::json() couldn't also be used to set the header as it would be like doing the encode twice and the quotes being escaped.

json_encode(json_encode( $array, JSON_FORCE_OBJECT))

Please or to participate in this conversation.