Change JSON/API Output

Hello everyone, i’m stuck with a problem right now. I want to change the output of the week-info API to match the requirements of fullcalendar, to display events on our homepage. Am I right in the ApiController.php? I want to change “name:” into “title:” and “starts:” into “start:” and “ends:” into “end:”.

Any help welcome, thank you!

I wouldn’t recommend making those changes in Libretime. What if you just loop into the week-info API and change the key names in your homepage instead?

Example:

  $result = [];
  if($original_result){
    
    foreach($original_result as $key => $val){
       foreach($val as $value){
           
        $result[$key][] = [
            "title"=> $value['name'],
            "start"=> $value['starts'],
            "end"=> $value['ends']
         ];
         
        }
    }

  $new_result = $result;
    
}

Just incase you need to call a url, you can create a new php file on your site:

  header('Content-Type: application/json');
  $parsed = json_decode(file_get_contents("your-server/api/week-info"), true);
  $result = [];
  if($parsed['result']){
    
    foreach($parsed['result'] as $key => $val){
       foreach($val as $value){
           
        $result[$key][] = [
            "title"=> $value['name'],
            "start"=> $value['starts'],
            "end"=> $value['ends']
         ];
         
        }
    }
  }
  //new result
  echo json_encode($result);

And now you can call this file instead.

Hey codenift,

thanks for the answer and the given examples. I got it working by tweaking the code a little bit. Unfortunately FullCalendar isn’t able to read the resulting data. I guess it has to do with the structure and that for every day (monday, tuesday…) there is a own JSON-Object. Do you know about this? And is there a way to merge all of those objects into one?

In that case just remove the key. So it will look like this instead:

 foreach($parsed as $key => $val){
   foreach($val as $value){
       
    $result[] = [ //this line 
        "title"=> $value['name'],
        "start"=> $value['starts'],
        "end"=> $value['ends']
     ];
     
    }
}

That did the “magic”. Thanks a lot!

1 Like

Hello,

Im using this code but is not outputting anything:

<?php
header('Content-Type: application/json');
$parsed = json_decode(file_get_contents("https://lumbungradio.stationofcommons.org/api/week-info/"), true);
$result = [];
if($parsed['result']?? null){

  foreach($parsed as $key => $val){
    foreach($val as $value){

      $result[] = [ //this line
        "title"=> $value['name'],
        "start"=> $value['starts'],
        "end"=> $value['ends']
      ];

    }
  }
}
//new result
echo json_encode($result);