SOLVED: Simple JSON list from page data

Permalink
I need to create a simple JSON list from site page data. It needs to look like this:

[
   {
      "title": "Page title",
      "description": "Short description would go here",
      "image": "/path/to/page-thumbnail.jpg",
      "link": "http://link-to-a-specified-site.com/"
   },
   {
      "title": "Page 2 title",
      "description": "Short description for page 2",
      "image": "/path/to/page2-thumbnail.jpg",
      "link": "http://www.google.com/"
   }
]


Each entry should represent data from a single page, and these would be all children of one parent page.

I then need to be able to access this at a specified URL.

Can anyone point me in the right direction for best practice using C5 here, or has anyone done similar previously?

Thanks.

drbiskit
 
shahroq replied on at Permalink Reply
shahroq
C5 has a built-in JSON helper, check the docs here:
http://www.concrete5.org/documentation/developers/helpers/json...

about the json string result you want, first store the data at the array and then convert it to json string with json helper:
$tmp = array();
$tmp[] = array("title" => "Page title",
               "description"=>"Short description would go here",
               "image"=>"/path/to/page-thumbnail.jpg",
               "link"=>"http://link-to-a-specified-site.com/"
               );
$tmp[] = array("title" => "Page 2 title",
               "description"=>"Short description for page 2",
               "image"=>"/path/to/page2-thumbnail.jpg",
               "link"=>"http://www.google.com/"
               );
$js = Loader::helper('json');
$r = $js->encode($tmp);
echo $r;

the result should be what you are looking for.
drbiskit replied on at Permalink Reply
drbiskit
Hi - Thanks for taking the time to answer, and apologies I didn't respond sooner. I had to down tools on this for a couple of weeks...

So, I actually ended up just producing a single page with a custom page list block, that has done the trick nicely.

Thanks for this info about the JSON helper though - useful to know!