thmsnhl

Clean way to output data as json

7 posts in this topic

Hi everyone,

I've recently hired at a new company and here I am evaluating the abilities of ProcessWire for our projects.
I was able to meet almost every requirement so far, but there is one point I couldn't find an adequate solution for: outputting data to json.

I am aware of modules like https://modules.processwire.com/modules/pages2-json/ (which does not seem to work for me) but I thought with a function like wireEncodeJSON this should be much cleaner. What I would like to achieve is outputting pages with according field values into an array to use this within javascript.
My first attempt on this was:

$jsontestOne = $pages->find(1001)->children();
echo wireEncodeJSON($jsontestOne);

which outputs 

[{}]

and afterwards I tried that one:

$jsontest = $pages->find("template=basic-page")->getArray();
echo wireEncodeJSON($jsontest);

which outputs 

[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},...]

Maybe you can point out where my mistake is.
 

Thanks in advance!

Share this post


Link to post
Share on other sites

Hi @thmsnhl

Have you seen this thread? 

There is an example that you may find useful

$events = $pages->find("template=sectionItem, parent=1025|1066|1073|1069|1013|1247|1101, sort=startTime, start=10, limit=3");
$events_array = array();

foreach ($events as $event) {
    
    $start = "".date(strtotime($event->startTime))."";
    $title = $event->title;
    
    $events_array[] = array(
        'title' => $title, 
        'date' => $start
    );
}

$events_json = json_encode($events_array, true);
echo $events_json;

 

Share this post


Link to post
Share on other sites

the problem is that you have an array, but the items in the array are \ProcessWire\Page objects, so those can't directly map to a json encode.

you probably need to cycle through the pages you want and create the array manually. Alternately you could have a look at the GraphQL module, which i think some devs are using to get json data to the frontend.

3 people like this

Share this post


Link to post
Share on other sites

Thank you @Zeka for this thread I just realized that I had this already open in a tab but waaaaaay too far on the right hand side of the window, I will try to fit the example in to my project.
But first I will have a look into GraphQL because this might also help me with future requirements. 

Share this post


Link to post
Share on other sites

For simple json outputs, you can use WireArray::explode and json_encode() or wireEncodeJSON() methods

https://processwire.com/api/ref/wire-array/explode/

$myPages = $pages->find('template=basic-page');
// extract required fields into plain array
$data = $myPages->explode(['title', 'created']);
echo wireEncodeJSON($data);
6 people like this

Share this post


Link to post
Share on other sites

For more advanced uses (api or even json-api by specification) I'd recommend something like fractal.

2 people like this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By beto
      I can't add a new value to a custom PageArray field  for my user.
       
      However, when I create a new user, I CAN add the new PageArray value:
       
      I need help. Thank you very much.
       
    • By bmacnaughton
      I would like to be able to fetch the labels for fields in a language different than the current logged in user.
      For field values that's easy 
      // p is page, l is language, and f is field $p->getLanguageValue($l, $f); I'm looking for something like
      $fields->getLanguageValue('en', 'length'); The only solution I know of is to save the current user language, iterate through the languages by setting the user language and fetching the value, and then restore the user language.
       
    • By bmacnaughton
      Is there a way to tell if a field is a multi-language field?
      I am currently checking to see if the last 8 characters are 'Language' but that seems like a fragile solution.
       
       
    • By louisstephens
      I was working on a simple "to-do" style template that has a form on a page. Once the form is submitted, I use the API to  create a new subpage under a pre-existing page, but I notice that it does resubmit the form data (as to be expected) if a user were to refresh the page. Is there a way to prevent this behavior so duplicate content will not be posted by using something like exit(), or is this not proper due to usability?  I guess I am curious as to how other developers handle the same hurdle.
    • By cosmicsafari
      Hi all,
      I am creating a page field (field of type FieldtypePage) via the API, however im still trying to find some documentation as to how I would go about setting the Selectable Pages for said field using the API.
      From what I have found it looks like it involves the use of, albeit this looks like a getter rather than a setter:
      $field->getInputfield($page) Which looks like it would make sense if I wanted to specify the selectable pages by a parent page, but what if I wanted to specify it by say a template?