October 21, 2024
Chicago 12, Melborne City, USA
PHP

json_encode is passing invalid data


A json payload is being sent from one website through my middleware and then to a final website.

The json object that is being sent looks like this:

{
  "event": "donation_completed",
  "payload": {
    "email": "myemail@yahoo.com",
    "fields": {
      "mrcC3": "1.05",
      "country": "US",
      "lastName": "Beasley",
      "firstName": "John",
      "mrcDateC3": "2024-10-21 03:06:24 UTC"
    },
    "overwrite": true
  }
}

In Postman, I can print_r the object above like this:

print_r($payload);

This is the results:

Array
(
[event] => donation_completed
[payload] => Array
(
[email] => myemail@yahoo.com
[fields] => Array
(
[mrcC3] => 1.05
[country] => US
[lastName] => Beasley
[firstName] => John
[mrcDateC3] => 2024-10-21 03:06:24 UTC
)
[overwrite] => 1
)
)

In the PHP script, I decode the json like this:

<?php
$payload = json_decode(file_get_contents("php://input"), true);

I can then set each of the json items into their own variable like this:

$event = $payload["event"];
$email = $payload['payload']["email"];
$firstName = $payload['payload']['fields']['firstName'];
$lastName = $payload['payload']['fields']['lastName'];
$country = $payload['payload']['fields']['country'];
$mrc_c3 = $payload['payload']['fields']['mrcC3'];
$number = floatval($mrc_c3); <-- me trying to ensure the mrcC3 is not a string
$mrc_date_c3 = date('m/d/Y', strtotime($payload['payload']['fields']['mrcDateC3']));
$mrc_date_c3_format = str_replace("\"", "", $mrc_date_c3); <-- me trying to ensure the date format is correct

I can echo each of the items above in Postman like this:

echo "first name " . $firstName; // <-- prints "first name John"
echo "date is " . $mrc_date_c3_format; // <-- prints "date is 10/21/2024"
echo "mrcC3 is " . $number; // <-- prints "mrcC3 is 1.05"

I build the array which is to be sent to the final website. It looks like this:

 $arr = [
    'event' => $event,
    'payload' => [
        'email' => $email,
        'fields' => [
            'mrc_c3' => $number,
            'country' => $country,
            'last_name' => $lastName,
            'first_name' => $firstName,
            'mrc_date_c3' => $mrc_date_c3_format,
        ],
    'overwrite' => true,
    ],
];

I use json_encode to send the array like this:

json_encode($arr, JSON_UNESCAPED_SLASHES) // <-- using JSON_UNESCAPED_SLASHES to ensure the date is passed without any backslashes

Everything above works fine in Postman. Postman acts as the first website sending the data over to the middleware, and then to the final website and it saves as it should.

The problem I am experiencing is when I send the same payload from the first website.

The json is initially sent and looks just like the json at the top of this question.

But when it hits the middleware, I can print out the results from an option in the first website, and it looks like this:

"{\"event\":\"donation_completed\",\"payload\": 
{\"email\":\"myemail@yahoo.com\",
\"fields\":{\"mrc_c3\":null,\"country\":\"US\",\"last_name\":null,
\"first_name\":null,\"mrc_date_c3\":\"01/01/1970\"},\"overwrite\":true}}"

I do not understand why this is happening. Why is the mrc_c3 showing as null? Why is the date showing as the default date? Why is the first and last name null but the email and country is not?

Am I not setting the variables correctly?

I asked another question like this, and I was told to build a PHP array and json_encode it. Which leads me to ask this question.

What am I doing wrong?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video