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

Aronaman's avatar

Structure return of json

hello guys, how can I return custom Json structure, in laravel instead using normal method like

//normal method
'title' => 'foo',
'body' => 'bar',

What I want to achieve

customeJson = {  
	"name": "","
	fields": { 
 		"items": {},
		"Date": {
      	 "timestampValue": $date
 		 },
 		"bill_no": {
        	"stringValue": $billNo
  		},
 	}
 }
return customeJson;

Thanks for your time

0 likes
5 replies
tykus's avatar

I don't know what you mean by normal method; but using an associative array, you can specify the JSON structure in PHP and pass to the json Response method:

return response()->json([
	'title' => 'foo',
    'body' => 'bar',
]);
Aronaman's avatar

@tykus thanks for response , i know this method, my question is how can I Structure "Date": { "timestampValue": $date }, this kind of json in laravel

tykus's avatar
tykus
Best Answer
Level 104

@Aronaman

return response()->json([
	'Date' => [
        'timestampValue' => $date
    ],
]);
1 like
thgs's avatar

@Aronaman If you mean that you want to use : instead of => and {} instead of [], you have to choose a framework that is not written in php

Aronaman's avatar

@thgs yea I am working in different frameworks that why it mix up, thanks

Please or to participate in this conversation.