Export all site content as JSON

Copied from old forum: /forum/code-snippets/20150228/export-all-site-content-as-json

So as Im not much into PHP and very much in to JavaScript, its more suitable for me to get all data from Kirby as JSON. And then later I load JSON in html5/javascript project, for example Adobe Edge Animate project.

As base I used this guide about exporting to JSON: http://getkirby.com/blog/json
but a bit updated so I can get whole content data in JSON.

For note - made that JSON generates two objects - data and params. Also added, that on JSON request it generates thumbnail for each added image in content.

You need to add file jsonapi.txt under yoursite/content/api and following code is file jsonapi.php in yoursite/site/templates

header('Content-type: application/json; charset=utf-8');

//this is the magic command which grabs all data from pages. If You wanna grab specific page articles, look at the base example mentioned above
$data = $pages->index();

$json = array();

$json['data']  = array();
$json['params']  = array();

$i = 0;

foreach($data as $article) {

  $images = array();

  foreach($article->images() as $image) {

    //this is Bastians snippet about gathering image data
    $images[] = array(
      'url'    => $image->url(),
      'width'  => $image->width(),
      'height' => $image->height(),
      'thumb' => thumb($image, array('width' => 290, 'height' => 290, 'crop' => true))->url()

  $json['data'][] = array(
    'title' => (string)$article->title(),
    'text'  => (string)$article->text(),
    'images'  => $images

  // this cleans up JSONS code so there is no empty entries inside objects
  $cleanData = array_filter($json['data'][$i]);
  $json['data'][$i] = $cleanData;

$foo = $json['params'];

  //just example of boolean. in blueprints its type: checkboxes
  if ((string)$article->adress()) { 
      $foo['adress'] = true;

//example of string as param. in blueprints its type: number
if ((string)$article->time()) {
      $foo['time'] = (string)$article->time();

  $json['params'] = (object)$foo;

echo json_encode($json);

So JSON looks something like this after calling: http://localhost/yoursite/api

{"data":[{"title":"some title for test use","text":"some text for test use"},"images":[{"url":"http://localhost/yoursite/content/1-page/some_image.jpg","width":1024,"height":640,"thumb":"http://localhost/yoursite/thumbs/some_image.jpg"}]},{"title":"api"},{"title":"Error"},{"title":"Home"}],"params":{"adress":true,"time":"45"}}

Probably Im the only one who make such approach and use of Kirby, but its really suitable for me. Hope that it will be useful for someone too.