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:

[{"url":"http:\/\/hashandsalt.dev"},{"url":"http:\/\/hashandsalt.dev\/services"},{"url":"http:\/\/hashandsalt.dev\/hire-us"},{"url":"http:\/\/hashandsalt.dev\/blog"}]

What i actually need is this:

"http:\/\/hashandsalt.dev","http:\/\/hashandsalt.dev\/services","http:\/\/hashandsalt.dev\/hire-us","http:\/\/hashandsalt.dev\/works"

The code that generates this:

<?php
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:

Thanks!

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: