Did you try
$jsonData = json_decode(json_encode($data, JSON_FORCE_OBJECT));
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a api I am trying to send to. It requires empty arrays to have curly brackets "{}" instead of regular brackets "[]".
Here is my code that i am using to post to the api:
$sendReport = Http::withToken($authResponse)
->contentType('application/json')
->post($crashExportServer . $uploadPoint, $data)
->json();
$data = an array. This works, but I have the issue with the empty arrays within $data. I get an error back in regards to them. I can use json_encode and pass the parameter of "JSON_FORCE_OBJECT" and then it looks right to me. However, I'm stumped on how to do then when using the http client. I tried replacing the $data array with a string of the encoded JSON string, but that fails:
$jsonData = json_encode($data, JSON_FORCE_OBJECT);
$sendReport = Http::withToken($authResponse)
->contentType('application/json')
->post($crashExportServer . $uploadPoint, $jsonData)
->body();
Here, $jsonData is the output of, but the api doesn't like that at all. I also tried:
$jsonData = json_encode($data, JSON_FORCE_OBJECT)
$sendReport = Http::withToken($authResponse)
->contentType('application/json')
->post($crashExportServer . $uploadPoint, ['body' => $jsonData])
->body();
But the api doesn't seem to like that either.
Any help would be greatly appreciated! Thanks!
Please or to participate in this conversation.