Update problem: programatically create pages from local json file

I am updating from Kirby 2 to Kirby 3.
I have this code to programaticaly create pages from a local json file (cobbled together from suggestions from the forum, because my php skills are weak). Worked great in Kirby 2; it has stopped working in Kirby3.
The debugger says that the “Invalid argument supplied for foreach()”. I’ve been using an old Json file that was set up correctly, so there’s no problem there.

return function($site, $pages, $page) {
$alert = null;
$json = [];
if($page->file()) {
	$src = page()->file();
  	  if(r::is('post') && get('good')) {
  	  	$content = f::read($src);
  		$json = json_decode($content, true);
  		foreach($json['entrants'] as $entrant){
  			$data = array(
  			  'idno' => $entrant['idno'],
  			  'firstname' => $entrant['firstname'],
  			  'lastname' => $entrant['lastname']
  			$uid = $entrant['lastname'] . '-' . $entrant['firstname'] . '-' . $entrant['idno']; 
  			$template = 'gallery-project';
  			try {
  				$folder = page()->parent();			    	
  				$newPage = $folder->children()->create($uid, $template, $data);
  			  	$success = 'The new pages have been created';
  			  echo 'The new page for '. $uid .' has been created<br>';
			} catch(Exception $e) {
		  		echo 'Page creation has failed: ' . $e->getMessage();
return compact('alert', 'src','folder','newPage', 'success');

i’m not sure, but the toolkit functions have changed quite a bit,


seeing the error, it should fail prior to the foreach… so the file might not be read correctly.
you can debug step for step and output


as well as


make also sure to have proper rights to create a page…


F::read() expects a path to a file, not a file object.

Apart from that, I’d suggest to modify the code a bit

if($src = $page->file()) {
  if(r::is('post') && get('good')) {
    $content = f::read($src->root());