This topic is locked
[SOLVED]

 JSON Result Into Table

4/2/2020 11:08:48 AM
PHPRunner General questions
S
salus1 authorDevClub member

Hi,

What is the correct way to generate a key/value table from the JSON result? I'd like to read fixer.io currency exchange rates...
{

success: true,

timestamp: 1585838946,

base: USD,

date: 2020-04-02,

rates: {

AED: 3.67295,

AFN: 76.150514,

ALL: 117.000238,

AMD: 504.21976,

ANG: 1.797937,

AOA: 536.679802, etc...
... into a look-up table but can't figure out how to make multiple rows instead of all results in single row.
Screenshot attached.

Sergey Kornilov admin 4/3/2020

The problem here is that multiple currencies sit in a single field named "rates" while it should be an array and there should be a separate entry for each currency. Something like this would be a more traditional format.

{

success: true,

timestamp: 1585838946,

base: USD,

date: 2020-04-02,

rates: [

{ currency: AED, rate: 3.67295 } ,

{ currency: AFN, rate: 76.150514 } ,

{ currency: ALL, rate: 117.000238 }

...

],

...


It is still possible to make it work with this kind of data format. After you have setup the initial connection you can switch to 'PHP code' mode, parse this JSON and present it as an array.
Take a look at 'Any PHP or C# code' section at https://xlinesoft.com/blog/2020/03/10/version-10-4/

S
salus1 authorDevClub member 4/3/2020

Thanks Sergey. I went ahead and built an intermediary file that runs the query and parses the result into the required JSON format.
Place this in a password protected folder on your server and use HTTP Basic authentication with the REST API connection URL to access it.
<?php

header("Access-Control-Allow-Origin: *");

header("Content-Type: application/json; charset=utf8mb4");

header("Cache-Control: no-cache, must-revalidate");

header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

$curl = curl_init();

curl_setopt_array($curl, array(

CURLOPT_URL => "https://data.fixer.io/api/latest?access_key=YOURFREEACCESSKEY&base=USD";,

CURLOPT_RETURNTRANSFER => true,

CURLOPT_TIMEOUT => 30,

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

CURLOPT_CUSTOMREQUEST => "GET",

CURLOPT_HTTPHEADER => array("cache-control: no-cache"),

));

$response = curl_exec($curl);

$response = substr($response,stripos($response,"rates")+7);

$response = str_replace(",","},{\"currency\" = ",$response);

$response = str_replace(":",", \"rate\" = \"",$response);

$response = str_replace("{\"AED","[{\"currency\" = \"AED",$response);

$response = str_replace(" = "," : ",$response);

$response = str_replace("}}","\"}]",$response);

$response = str_replace("},{","\"},{",$response);

//$json = json_encode($response);

curl_close($curl);

print_r($response);

?>