Accessing my JSON data

Hi,
I currently have a lot of data in a JSON file. Currently I’m pulling that data into a gallery/lightbox. What I’d like to do is get rid of the gallery/lightbox and set up a new way of showing the data.

I would like each data entry have its own page. Because of the large amount of entries in the JSON file, I obviously couldn’t create all the necessary folders/files by hand. And I would also want a way to link to an specific page. Is there a way to dynamically create a URL for that page?

I hope I’ve explained what I want to do clearly. Thanks for all the help :slight_smile:

You could create the folders/files programmatically, using the $pages->create() method.

thanks for your reply. It’s the solution I want, but because I am a php noob, I can’t figure out how to implement it.
So far I’ve got:

       $src = page()->file('annual13.json')->url();
       $body=preg_replace('/.+?({.+}).+/','$1',$src); 
       $content = file_get_contents($body);
       $json = json_decode($content, true);
       ?>
       <?php foreach($json['entrants'] as $entrant): ?>
          $data = array(
         'title' => <?php echo $entrant['name'] ?>,
         'category' => <?php echo $entrant['category'] ?>,
          'url' => <?php echo $entrant['url'] ?>
         );
       <?php endforeach ?>```

My JSON looks like this:

```{ "entrants" : [
    { "name" : "Jane" , 
      "category" : "gallery",
     "url" : "google.com"} , 
     { "name" : "Joe" , 
      "category" : "editorial",
      "url" : "cnn.com"}]}```


 I can't figure out how to incorporate that into the pages->create() method.
1 Like

You need a unique UID, whatever you use to make that up:

<?php foreach($json['entrants'] as $entrant): ?>
<?php
$data = array(
'title' => $entrant['name'],
'category' => $entrant['category'],
'url' => $entrant['url']
);
// you need to add something here, a number or whatever to make this unique, 
//as the name alone will probably be repetitive
$uid = $entrant['name']; 
// the template you want to use for the new pages
$template = 'default';


// try to create the pages
try {

  $newPage = page('name_of_parent_page')->children()->create($uid, $template, $data);

  echo 'The new page has been created';

} catch(Exception $e) {

  echo $e->getMessage();

}
?>
<?php endforeach ?>
1 Like

Oh, so simple! Thanks for explaining it!

You are welcome. If you need any more help with refining it, don’t hesitate to ask :slight_smile:.