Jump to content

How to convert JSON data inside PHP variable into readable format in table


Recommended Posts

Hi, I have a connection set up to an API using a PHP script - the API sends back data in JSON format, and if I capture it and echo it, it displays in extremely unfriendly format on the screen.

I've tried to find a way to convert this data into readable format, ideally in a HTML table, but although there are articles on converting manually inputted JSON data into a HTML table using Javascript, I can't find a way to do it from a PHP variable.

This is what I have so far:

if ($_POST['getcompany']) {
$companyname = $_POST['_Name'];
	
$ch = curl_init();

$data_array2 =  array(
      'token' => $token
);
$make_call2 = json_encode($data_array2);
	//echo 'Token is '.$token;
	$token2 = substr($token, 14);
	$token3 = substr($token2, 0, -3);
	
	//echo 'Token 2 is '.$token3;
	//echo 'Company Name is '.$companyname;

//curl_setopt($ch, CURLOPT_GET, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $make_call2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: '.$token3.'')); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://connectionurl?countries=GB&name='.$companyname);

//Execute the request
$result = curl_exec($ch);

echo 'Companies: '.$result; //This displays the data provided by the search, but is in JSON format

 

So I need a way to get the data held in $result, into a HTML table. Any idea how I can do this?

Thanks, I already used json_encode above, should I be using it on $result as well? i.e $resultencoded = json_encode($result);

I tried that but it just seems to add slashes in front of the quotation marks?

Also tried this:

<pre><?php echo json_encode($result JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); ?></pre>

and

<pre><?php echo json_encode($result JSON_PRETTY_PRINT); ?></pre>

But it still gives an unfriendly output, putting lots of instances of \n in place?

Edited by speckytwat2
22 minutes ago, speckytwat2 said:

echo 'Companies: '.$result; //This displays the data provided by the search, but is in JSON format

Sounds like you need json_decode()

Edited by Barand
6 minutes ago, Barand said:

Sounds like you need json_decode()

Thanks... unfortunately I tried that:

$decoded = json_decode($result, true);

and

$decoded = json_decode($result);

The 1st just outputted "Array" and the 2nd threw an error: "Fatal error: Uncaught Error: Cannot use object of type stdClass as array in..." then "Stack trace: #0 {main} thrown in..."

I also tried this:

$decoded = json_decode($result, JSON_PRETTY_PRINT);

But that still outputs the same data, albeit with Array => instances added.

Just need a way to get this somehow exported to a HTML table?

Edited by speckytwat2
On 1/29/2021 at 2:56 PM, gw1500se said:

Sorry, I didn't realize that was what you wanted to do. Someone has already done that for you on github.

Thanks, looking at that though the only problem is the JSON data being pulled from the API is going to be different each time, so I can't see how it would work- how would I throw all the raw JSON data into the objectArray variable? I can put it into a PHP variable and in fact tried creating a JSON file from that, so it would have whatever the queried data was on that occasion:

$fp = fopen('result.json', 'w');
fwrite($fp, $result); //Where $result is the data pulled through from the API

But I don't think the example script can read a specially created JSON file?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.