Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Respect's avatar

How to remove brackets wrapper in api resource responses

Hello friends How to remove brackets wrapper in api resource responses for example now

{
    "data": {
	 .........
  	}	
}

i NEED TO DO IT LIKE THIS

"data": {
	 .........
  	}	
0 likes
10 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Json needs a root.. So brackets are needed. Why would they need to be removed?

1 like
Respect's avatar

@Sinnbeck Android Developer Need do that shape in response so it's it's not possibale sir ?!!!

Sinnbeck's avatar

You can try opening your browser console (F12->console). Paste this in and hit enter

const myvar = {
    "data": {
	  "bar": "foo",
  	}	
};

You can now access data on myvar.data

Respect's avatar

@Sinnbeck

i got this response

 {
   "data":{
      "result":"success",
      "message":"",
      "data":[
         {
            "id":1,
            "name":"Category Number 0",
            "icon":"path",
            "color":"blue"
         },
         {
            "id":2,
            "name":"Category Number 0",
            "icon":"path",
            "color":"red"
         },

      ]
   }
}

i need to remove first {} to make the data as first object look that

"data":{
      "result":"success",
      "message":"",
      "data":[
         {
            "id":1,
            "name":"Category Number 0",
            "icon":"path",
            "color":"blue"
         },
         {
            "id":2,
            "name":"Category Number 0",
            "icon":"path",
            "color":"red"
         },

      ]
   }

*code in controller

$categories = Category::select('id',toLocale('name'),'color')->get();
// just to can append result and message 
  return response()->json(['data' => new CategoryCollection($categories)], 200);
   // return new CategoryCollection($categories);
  • code in api resource
 return  [
            'result' => 'success',
            'message' => '',
            'data' => $this->collection,
        ];
Sinnbeck's avatar

@Respect It already is as you want it. The other {} just tells the JSON parser that its an object.

1 like
Sinnbeck's avatar

Does the android developer perhaps mean that it should not be wrapped in data?

{
      "result":"success",
      "message":"",
      "data":[
         {
            "id":1,
            "name":"Category Number 0",
            "icon":"path",
            "color":"blue"
         },
         {
            "id":2,
            "name":"Category Number 0",
            "icon":"path",
            "color":"red"
         },

      ]
1 like
Respect's avatar

@Sinnbeckfirst of all thanks sir for your time and help - i sent the answer to him and waiting his answer can i keep the answer open untill i get his answer back than i will mark it okey sir

Sinnbeck's avatar

@Respect Maybe send him the link to this thread? Then he can perhaps come and explain what he needs :)

1 like
Respect's avatar

@Sinnbeck ok sir i will send it to him --- Thanks Again for your time and help later will mark your answer

Please or to participate in this conversation.