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

onairmarc's avatar

Unable to process incoming JSON Data

Hi All,

I'm running into an issue where I cannot seem to get the 'value' key of the POSTED JSON to my API Endpoint. I've spent several hours trying to troubleshoot this but just can't seem to figure it out. Can anyone please take a look and let me know what I might be missing?

namespace App\Http\Controllers\Email;

use App\Http\Controllers\Controller;
use App\Console\Commands\Email\ChangeNotifications\ProcessEmailChangeNotificationCommand;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;

class EmailChangeNotificationController extends Controller
{
    public function index(Request $request)
    {
        if (request()->query('validationToken')) {
            return request()->query('validationToken');
        } else {
            $jsonData = $request->json()->all();

            if (isset($jsonData['value']) && is_array($jsonData['value'])) {
                $firstValue = $jsonData['value'][0];

                $subscriptionId = $firstValue['subscriptionId'];
                $changeType = $firstValue['changeType'];

                if (isset($firstValue['resourceData']['id'])) {
                    $resourceId = $firstValue['resourceData']['id'];
                } else {
                    return response()->json(['success' => false, 'message' => 'resourceData.id is not set'], 400);
                }

                $tenantId = $firstValue['tenantId'];

                Artisan::call(ProcessEmailChangeNotificationCommand::class, [
                    '--resourceId' => $resourceId,
                    '--subscriptionId' => $subscriptionId,
                    '--changeType' => $changeType,
                ]);
                return response()->json(['success' => true]);
            } else {
                return response()->json(['success' => false, 'message' => 'value is not set'], 400);
            }
        }
    }
}

Here's the JSON that is being posted:

{
    "value": [
        {
            "subscriptionId": "string",
            "subscriptionExpirationDateTime": "2023-10-17T17:00:28.854381+00:00",
            "changeType": "updated",
            "resource": "string",
            "resourceData": {
                "@odata.type": "#Microsoft.Graph.Message",
                "@odata.id": "string",
                "@odata.etag": "string",
                "id": "string"
            },
            "clientState": null,
            "tenantId": "string"
        }
    ]
}
0 likes
6 replies
onairmarc's avatar

Upon further inspection the following var_dump($jsonData) returns NULL

$jsonData = $request->json()->all();
var_dump($jsonData);

I've also tried this

$jsonData = $request->getContent();
var_dump($jsonData);

Returns:

string(1197) "
{
    "value": [
        {
            "subscriptionId": "string",
            "subscriptionExpirationDateTime": "2023-10-17T17:00:28.854381+00:00",
            "changeType": "updated",
            "resource": "string",
            "resourceData": {
                "@odata.type": "#Microsoft.Graph.Message",
                "@odata.id": "string",
                "@odata.etag": "string",
                "id": "string"
            },
            "clientState": null,
            "tenantId": "string"
        }
    ]
}"

Running json_decode($jsonData) on this method also does not work.

jlrdw's avatar

@onairmarc

        $jsonData = '{
    "value": [
        {
            "subscriptionId": "string",
            "subscriptionExpirationDateTime": "2023-10-17T17:00:28.854381+00:00",
            "changeType": "updated",
            "resource": "string",
            "resourceData": {
                "@odata.type": "#Microsoft.Graph.Message",
                "@odata.id": "string",
                "@odata.etag": "string",
                "id": "string"
            },
            "clientState": null,
            "tenantId": "string"
        }
    ]
}';
        //dd($jsonData);
        
        $mydata = json_decode($jsonData, true);
          //dd($mydata["value"]);    
        
        $firstValue = $mydata["value"][0]["subscriptionId"];
        echo $firstValue;
        die;

Works.

Just a quick test I did. Edit it can take a little trial and error for iterating json data or array data.

1 like
onairmarc's avatar

@jlrdw Turns out there were larger issues at play. The JSON being sent from Microsoft was invalid causing the json_decode function to error out returning null. I had to write a preg_replace regex in order to remove the invalid json value.

Fun time...

jlrdw's avatar

@onairmarc

{
    "value": [
        {
            "subscriptionId": "string",
            "subscriptionExpirationDateTime": "2023-10-17T17:00:28.854381+00:00",
            "changeType": "updated",
            "resource": "string",
            "resourceData": {
                "@odata.type": "#Microsoft.Graph.Message",
                "@odata.id": "string",
                "@odata.etag": "string",
                "id": "string"
            },
            "clientState": null,
            "tenantId": "string"
        }
    ]
}

Is valid json. I checked it on https://jsonlint.com/

1 like
onairmarc's avatar

@jlrdw Yes, the redacted version of the JSON I posted is valid, however, the value of @odata.etag was "W/"rest_of_string"" which is what caused the issue. It was staring me in the face the entire time 🤦‍♂️

1 like

Please or to participate in this conversation.