How to pass JSON API results from php to Datatables?

How to pass JSON API results from php to Datatables?

michelmirmichelmir Posts: 16Questions: 7Answers: 0

Hello for all,

I'm working with an API payment and i would like that Datatables loads these API results on table. In this case, i have one php Curl script that receive JSON results and display all these results on page. Below are the Curl script:

<?php

$token = "TOKEN_PROD";

$ch = curl_init('URL_API');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $token
   ));
$data = curl_exec($ch); 

$info = curl_getinfo($ch);

$decode = json_decode( $data, true);
    
foreach ( $decode["results"] as $value){
        echo 'Id:'.$value["id"].PHP_EOL.'<br>';
        echo 'Date.:'.$value["date_created"].PHP_EOL.'<br>';
        echo 'Type.:'.$value["payment_type_id"].PHP_EOL.'<br>';
        echo 'Amount:'.$value["transaction_details"]["total_paid_amount"].PHP_EOL.'<br>';
        echo PHP_EOL.'<br>';
    }

curl_close($ch);


<?php
>
```
When i execute the php curl script above, all results from API display on page correctly and Json results from API, comes as below:
?>


{
"results": [
{
"metadata": {},
"corporation_id": null,
"operation_type": "regular_payment",
"fee_details": [],
"notification_url": null,
"date_approved": null,
"money_release_schema": null,
"payer": {
"first_name": "Test",
"last_name": "Test",
"email": "test_user_80507629@testuser.com",
"identification": {
"number": "32659430",
"type": "DNI"
},
"phone": {
"area_code": "01",
"number": "1111-1111",
"extension": ""
},
"type": "registered",
"entity_type": null,
"id": "660118681"
},
"transaction_details": {
"total_paid_amount": 10,
"acquirer_reference": null,
"installment_amount": 0,
"financial_institution": "",
"net_received_amount": 0,
"overpaid_amount": 0,
"payable_deferral_period": null,
"payment_method_reference_id": "1229234987916",
"verification_code": "1229982347916"
},
"statement_descriptor": null,
"call_for_authorize_id": null,
"installments": 1,
"pos_id": null,
"external_reference": null,
"date_of_expiration": "2020-10-20T22:59:59.000-04:00",
"charges_details": [],
"id": 1229987916,
"payment_type_id": "ticket",
"barcode": {
"content": "23791841400000010003323423480250122998791600633330"
},
"order": {
"id": "1886191696336",
"type": "mercadopago"
},
"counter_currency": null,
"brand_id": null,
"status_detail": "pending_waiting_payment",
"differential_pricing_id": null,
"additional_info": {
"ip_address": "123.456.789.10",
"nsu_processadora": null,
"available_balance": null
},
"live_mode": false,
"marketplace_owner": null,
"card": {},
"integrator_id": null,
"status": "pending",
"transaction_amount_refunded": 0,
"transaction_amount": 10,
"description": "Green Short",
"money_release_date": null,
"merchant_number": null,
"refunds": [],
"authorization_code": null,
"captured": true,
"collector_id": 582345463946,
"merchant_account_id": null,
"taxes_amount": 0,
"date_last_updated": "2020-10-17T11:30:31.000-04:00",
"coupon_amount": 0,
"store_id": null,
"date_created": "2020-10-17T11:30:31.000-04:00",
"acquirer_reconciliation": [],
"sponsor_id": null,
"shipping_amount": 0,
"issuer_id": null,
"payment_method_id": "ticket",
"binary_mode": false,
"platform_id": null,
"deduction_schema": null,
"processing_mode": "aggregator",
"currency_id": "USA",
"shipping_cost": 0
}
]
}



But if i want to display just some objects from json results above inside Datatable, i get an json error message from Datatable. Below are the script on homepage that i'm using to populate Datatable:
$(document).ready(function() { $('#example').DataTable({ "ajax": 'api_payments.php', "columns": [ { "data": "id" } { "data": "date_created" } { "data": "payment_type_id" } { "data": "total_paid_amount" } ] }); });

```
In this case, how can i improve my php script to display these json results inside datatables? Thanks :)

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    Using this as an example:

     { "data": "total_paid_amount" }
    

    That's nested within transaction_details - so you'll need to do something like

     { "data": "transaction_details.total_paid_amount" }
    
    

    as shown in this example here,

    Colin

  • michelmirmichelmir Posts: 16Questions: 7Answers: 0

    Hello Colin. Thanks for your feedback.

    Based on your orientation i found a way to solve my doubt. Thanks a lot :smiley:

This discussion has been closed.