JSON Code Generator?

2 posts by 2 authors in: Forums > CMS Builder
Last Post: October 25, 2020   (RSS)

By Toledoh - October 19, 2020

Hey Guys,

More out of interest rather than a specific project... so no rush to respond.

I often generate a php file and name it filename.xml.php and include the appropriate headers and structure. Can we do the same for JSON?

I can see we can use php to create a json file, but this means we have to run that file on a CRON to keep that JSON up to date.

<?php 

// data strored in array$array = Array (   
"0" => Array (
        "id" => "01",        "name" => "Tim Mason",        "designation" => "System Architect"    ),   
"1" => Array (
        "id" => "02",        "name" => "Jennifer Laurence",        "designation" => "Senior Programmer"    ),   
"2" => Array (
        "id" => "03",        "name" => "Medona Oliver",        "designation" => "Office Manager"    )
);

// encode array to json
$json = json_encode($array);
$bytes = file_put_contents("myfile.json", $json); 
echo "The number of bytes written are $bytes.";

?>

Cheers,

Tim (toledoh.com.au)

By Dave - October 25, 2020

Hi Tim, 

What about something like this? 

<?php
// example.json.php 

// data stored in array
$array = Array (   
"0" => [ "id" => "01", "name" => "Tim Mason",         "designation" => "System Architect",  ],   
"1" => [ "id" => "02", "name" => "Jennifer Laurence", "designation" => "Senior Programmer", ],   
"2" => [ "id" => "03", "name" => "Medona Oliver",     "designation" => "Office Manager",    ],
);

// output as json
header('Content-Type: application/json');
print json_encode($array);

// eof
Dave Edis - Senior Developer
interactivetools.com