Kirby API JSON site map

I have successfully setup Kirby’s API to generate a site map in JSON format, however I need it structured in a slightly different way and I have no idea how to achieve this.

I build my sites with Gulp and wish to use Uncss to optimise my stylesheets. That’s the reason for the JSON site map - Uncss uses this to read all the pages and work out the redundant CSS. I followed the cookbook and made some modifications to get my pages into JSON. The trouble is Kirby is generating it in this format:


What i actually need is this:


The code that generates this:

header('Content-type: application/json; charset=utf-8');
$root = $site->find('home');
$firstlevel = $site->children()->visible();
$json = array();
$json[] = array(
  'url' => (string)$root->url(),
foreach($firstlevel as $article) {
  $json[] = array(
    'url'   => (string)$article->url(),
echo json_encode($json);

How do i amend the above PHP so that it generates in the desired structure? Im getting tired of manually editing it… :frowning:


Try this:

foreach($firstlevel as $article) {
  $json[] =  (string)$article->url();

And the same for the root url.

Not tested.

Awesome! That worked a treat, thanks for the assistance :slight_smile: