Json_decode php function not working. result still json

Viewed 52

I'm getting from payment gateway response like this .

{"orders":[{"amount_charged":"0.00","status":"error","currency":"KZT","description":"dfd","updated":"2022-09-09 00:24:52","id":"71857529528744416","operations":[{"amount":"1512.00","created":"2022-09-09 00:24:52","type":"authorize","currency":"KZT","status":"error"}],"failure_message":"Internal bank error","amount_refunded":"0.00","created":"2022-09-09 00:24:34","merchant_order_id":"12","amount":"1512.00","pan":"489993****2157"}]}1

It look like json but 1 in end is extra . I dont know why it here . Ok, question isnt about it . I cut last character and result look like json. I try to decode it but

json_decode(substr($server_output, 0, -1),true);

result still json

{"orders":[{"created":"2022-09-09 00:24:34","merchant_order_id":"12","pan":"489993****2157","amount":"1512.00","description":"dfd","amount_charged":"0.00","status":"error","currency":"KZT","operations":[{"currency":"KZT","status":"error","type":"authorize","created":"2022-09-09 00:24:52","amount":"1512.00"}],"updated":"2022-09-09 00:24:52","id":"71857529528744416","failure_message":"Internal bank error","amount_refunded":"0.00"}]}

Why json_decode not working ?

1 Answers

works as expected, gonna need to share your code if your still having issues.

$server_output = '{"orders":[{"created":"2022-09-09 00:24:34","merchant_order_id":"12","pan":"489993****2157","amount":"1512.00","description":"dfd","amount_charged":"0.00","status":"error","currency":"KZT","operations":[{"currency":"KZT","status":"error","type":"authorize","created":"2022-09-09 00:24:52","amount":"1512.00"}],"updated":"2022-09-09 00:24:52","id":"71857529528744416","failure_message":"Internal bank error","amount_refunded":"0.00"}]}1';

$server_output = json_decode(substr($server_output, 0, -1), true);

[
  "orders" => [
    [
      "created" => "2022-09-09 00:24:34",
      "merchant_order_id" => "12",
      "pan" => "489993****2157",
      "amount" => "1512.00",
      "description" => "dfd",
      "amount_charged" => "0.00",
      "status" => "error",
      "currency" => "KZT",
      "operations" => [
        [
          "currency" => "KZT",
          "status" => "error",
          "type" => "authorize",
          "created" => "2022-09-09 00:24:52",
          "amount" => "1512.00",
        ],
      ],
      "updated" => "2022-09-09 00:24:52",
      "id" => "71857529528744416",
      "failure_message" => "Internal bank error",
      "amount_refunded" => "0.00",
    ],
  ],
]

Related