-1

Anyone can tell me why this return to the array to string conversation when I decoding it. I didn't see any error for this type of JSON.

{"transaction":{"token":"8mBjEEGt0E7QxhZoObDb8Jy0gSH","created_at":"2019-12-22T04:42:23Z","updated_at":"2019-12-22T04:42:24Z","succeeded":true,"transaction_type":"AddPaymentMethod","retained":false,"state":"succeeded","message_key":"messages.transaction_succeeded","message":"Succeeded!","payment_method":{"token":"TRYA1r9WQ0u8jllrJVHRjYyBOIh","created_at":"2019-12-22T04:42:23Z","updated_at":"2019-12-22T04:42:24Z","email":"fstancereq@virginia.edu","data":null,"storage_state":"cached","test":false,"metadata":null,"callback_url":null,"last_four_digits":"2214","first_six_digits":"475129","card_type":"visa","first_name":"Fleur","last_name":"Stancer","month":8,"year":2022,"address1":"46 Meadow Ridge Drive","address2":null,"city":"Orlando","state":"FL","zip":"32885","country":"US","phone_number":null,"company":null,"full_name":"Fleur Stancer","eligible_for_card_updater":true,"shipping_address1":null,"shipping_address2":null,"shipping_city":null,"shipping_state":null,"shipping_zip":null,"shipping_country":null,"shipping_phone_number":null,"payment_method_type":"credit_card","errors":[],"fingerprint":"fb8a1c8ddfb37ad4810335243495109596aa","verification_value":"XXX","number":"XXXX-XXXX-XXXX-2214"}}}

when I decode it using json_decode

echo $json["transaction"]["payment_method"];

I seek for the same problem but I didn't get an answer.

2 Answers2

1

There is nothing wrong with the JSON.

If you try to echo an array like a string, PHP will try to convert the array to a string.

Looking at the following JSON, $json["transaction"]["payment_method"] is not a string. It is an object or array depending on how you decode it.

{
  "transaction": {
    "token": "8mBjEEGt0E7QxhZoObDb8Jy0gSH",
    "created_at": "2019-12-22T04:42:23Z",
    "updated_at": "2019-12-22T04:42:24Z",
    "succeeded": true,
    "transaction_type": "AddPaymentMethod",
    "retained": false,
    "state": "succeeded",
    "message_key": "messages.transaction_succeeded",
    "message": "Succeeded!",
    "payment_method": {
      "token": "TRYA1r9WQ0u8jllrJVHRjYyBOIh",
      "created_at": "2019-12-22T04:42:23Z",
      "updated_at": "2019-12-22T04:42:24Z",
      "email": "fstancereq@virginia.edu",
      "data": null,
      "storage_state": "cached",
      "test": false,
      "metadata": null,
      "callback_url": null,
      "last_four_digits": "2214",
      "first_six_digits": "475129",
      "card_type": "visa",
      "first_name": "Fleur",
      "last_name": "Stancer",
      "month": 8,
      "year": 2022,
      "address1": "46 Meadow Ridge Drive",
      "address2": null,
      "city": "Orlando",
      "state": "FL",
      "zip": "32885",
      "country": "US",
      "phone_number": null,
      "company": null,
      "full_name": "Fleur Stancer",
      "eligible_for_card_updater": true,
      "shipping_address1": null,
      "shipping_address2": null,
      "shipping_city": null,
      "shipping_state": null,
      "shipping_zip": null,
      "shipping_country": null,
      "shipping_phone_number": null,
      "payment_method_type": "credit_card",
      "errors": [],
      "fingerprint": "fb8a1c8ddfb37ad4810335243495109596aa",
      "verification_value": "XXX",
      "number": "XXXX-XXXX-XXXX-2214"
    }
  }
}

You can output it using print_r($json); to see the decoded data.

Some other things you can do:

// Output a data element of payment_method
echo $json["transaction"]["payment_method"]["token"];

// Output all values
foreach($json['transaction']['payment_method'] as $k => $v) {
    echo "$k = $v\n";
}

Output to HTML

<?php
echo "<table>";
echo "<tr><th>Key</th><td>Value</td></tr>";
foreach($json['transaction']['payment_method'] as $k => $v) : ?>
    <tr>
      <td><?= $k ?></td>
      <td><?= $v ?></td>
    </tr>
<?php
endforeach;
echo "</table>";
?>
ryantxr
  • 4,119
  • 1
  • 11
  • 25
0

Your try to echo an array:

[
    "token" => "TRYA1r9WQ0u8jllrJVHRjYyBOIh",
    "created_at" => "2019-12-22T04:42:23Z",
    "updated_at" => "2019-12-22T04:42:24Z",
    // etc
]

Try the following command to get the value when you testing:

var_dump($json["transaction"]["payment_method"]);
Anatoliy R
  • 1,749
  • 2
  • 14
  • 20