Create JSON Through Kirby

Hello,

I’ve followed the tutorial (link: https://getkirby.com/docs/cookbook/json) on how to generate JSON, I’m wondering though how would i approach this if my PHP was to look like -

 <?php foreach($page->children() as $child): ?>
        <?php echo $child->title() ?>
           <?php foreach($child->images() as $img): ?>
                  <?php echo $img->url() ?>
           <?php endforeach ?>
 <?php endforeach ?>

This code has been simplified just to show the structure. Any help would be appreciated :slight_smile:

You can do it in the same way as described in this cookbook recipe: https://getkirby.com/docs/cookbook/ajax-load-more#the-json-template, using a snippet for the part within the foreach loop.

Hi Texnixe,

That’s great, I’m needing the final output to be a multidimensional array and be nested like so.

Would the code example you gave be able to produce this?

{
“logged_in”:true,
“town”:“Dublin”,
“state”:“Ohio”,
“country”:“USA”,
“products”:2,
“productinfo”:[
{
“0”:{
“pic_id”:“1500”,
“description”:“Picture of a computer”,
“localion”:“img.cloudimages.us/2012/06/02/computer.jpg”,
“type”:“jpg”,
“childrenimages”:2
},
“1”:{
“pic_id”:“15011”,
“description”:“Picture of a cpu”,
“localion”:“img.cloudimages.us/2012/06/02/mycpu.png”,
“type”:“png”
},
“2”:{
“pic_id”:“15012”,
“description”:“Picture of a cpu two”,
“localion”:“img.cloudimages.us/2012/06/02/thiscpu.png”,
“type”:“png”
},
“3”:{
“pic_id”:“1501”,
“description”:“Picture of a cpu”,
“localion”:“img.cloudimages.us/2012/06/02/cpu.png”,
“type”:“png”
}
}
]
}

Cheers

No, in your case, you need to generate the multidimensional array first and then encode to json. Otherwise, you won’t get the keys as required.

I have this so far which is returning the array but not as I would like :frowning:

                foreach($items->children() as $item) {

                    $data[] = $item->title();

                        foreach($item->fieldname()->yaml() as $i) {

                            $data[] = $i->url();
                            $data[] = $i->meta();
                        }
                    
                }
            
                print_r($data);

When encoded to json its being output as the following -

 "title":"Item title",
 "url":[
      "image URL here",
      "image URL here",
      "image URL here",
      "image URL here"
 ],
 "meta":[
       "meta info",
       "meta info",
       "meta info",
       "meta info"
 ]

This is from a folder with images, two subfolders. rather than looping through it seems to be grouping everything together rather than -

 "title":"Item title",
 "url":[
      "image URL here",
      "image URL here",
  ],
 "meta":[
       "meta info",
       "meta info",
 ]
 "title":"Item title",
 "url":[
      "image URL here",
      "image URL here",
  ],
 "meta":[
       "meta info",
       "meta info",
 ]

Any ideas?

I haven’t tested but I guess you need to create another array outside the foreach loop to hold the items:

$result = [];

foreach($items->children() as $item) {

    $data['title'] = $item->title()->value();

    foreach($item->fieldname()->structure() as $fieldname) {
        $data['url'][]  = $fieldname->url()->value();
        $data['meta'][] = $fieldname->meta()->value();
    }

    $result[] = $data;
}

dump($result);
// echo json_encode($result);

Somehow, the result of the above does not seem to work as expected, because the list of meta and url elements gets longer with each entry…

Here’s what I’ve come up with now:

<?php

$count = 0;
foreach($page->children() as $item) {

  $data[$count]['title'] = $item->title()->value();

  foreach($item->test()->structure() as $fieldname) {
      $data[$count]['url'][]  = $fieldname->url()->value();
      $data[$count]['meta'][] = $fieldname->meta()->value();
  }
  $count++;
}

dump($data);
?>
1 Like