Jump to content


Photo

export array to .json format


  • Please log in to reply
8 replies to this topic

#1 abe.izar

abe.izar

    Newbie

  • Members
  • Pip
  • 2 posts
  • 0

Posted 15 June 2012 - 07:26 AM

i am trying to save a page array as a .json string.

i tried:
$values = $pages->find("title=foo");
$jsonFile = wireEncodeJSON($values->getArray());
yet i simply get :
[{},{},{}]

i figured this didn't work becase the array has values which are objects, and they don't get converted properly.

i can probablly go through every child and convert it to an array and bulid the json manually, yet i wonder if there is an easier way to do this.

thanks :D

#2 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,526 posts
  • 854

  • LocationVihti, Finland

Posted 15 June 2012 - 08:02 AM

I guess the encode function wants page object instead of wirearray. Change find to get to see results?

#3 ryan

ryan

    Hero Member

  • Administrators
  • 5,774 posts
  • 3122

  • LocationAtlanta, GA

Posted 15 June 2012 - 08:07 AM

Welcome to the forums abe.izar. Seems like they should make json_encode at least attempt to typecast an object to a string (which would convert to IDs here). Maybe I can update wireEncodeJSON (a front-end to json_encode) do that. However, if you really want to export pages and all their data to JSON, here's how you can do it:


function pagesToJSON(PageArray $items) {
  $a = array();
  foreach($items as $item) {
    $a[] = pageToArray($item); 
  }
  return json_encode($a); 
}

function pageToArray(Page $page) {

  $outputFormatting = $page->outputFormatting;
  $page->setOutputFormatting(false);

  $data = array(
    'id' => $page->id,
    'parent_id' => $page->parent_id,
    'templates_id' => $page->templates_id,
    'name' => $page->name,
    'status' => $page->status,
    'sort' => $page->sort,
    'sortfield' => $page->sortfield,
    'numChildren' => $page->numChildren,
    'template' => $page->template->name,
    'parent' => $page->parent->path,
    'data' => array(),
    );

  foreach($page->template->fieldgroup as $field) {
    if($field->type instanceof FieldtypeFieldsetOpen) continue;
    $value = $page->get($field->name); 
    $data['data'][$field->name] = $field->type->sleepValue($page, $field, $value);
  }

  $page->setOutputFormatting($outputFormatting);

  return $data;
}


#4 abe.izar

abe.izar

    Newbie

  • Members
  • Pip
  • 2 posts
  • 0

Posted 21 June 2012 - 05:54 AM

thanks
works perfectly

#5 evanmcd

evanmcd

    Full Member

  • Members
  • PipPipPip
  • 82 posts
  • 28

Posted 09 July 2012 - 10:27 PM

Hi,

Thanks Ryan, for this snippet. I'll be using it all over in a web app I'm building using PW.

I've tried to make it into a module, but am running into an issue I confess I don't understand.

Could someone who knows the guts of modules in PW more than I take a look at what I've got so far?

The error I get is:

Method PageArray::toJSON does not exist or is not callable in this context (in /Projects/MyAppFolder/wire/core/Wire.php line 231)

Thanks!

#6 evanmcd

evanmcd

    Full Member

  • Members
  • PipPipPip
  • 82 posts
  • 28

Posted 09 July 2012 - 10:30 PM

Hmm, not sure my attachement came through. Here's the code for the module so far:

https://gist.github.com/3080802

#7 ryan

ryan

    Hero Member

  • Administrators
  • 5,774 posts
  • 3122

  • LocationAtlanta, GA

Posted 10 July 2012 - 09:00 AM

In your getModuleInfo() method, delete the "'permanent' => true" line, as that only makes it impossible to uninstall the module (which we only want for select core modules).

In your init() method, you are adding a 'toJSON' method to the $pages API variable. But your pagesToJSON() method appears to be written like it's meant to interact with a PageArray. As a result, I think the problem is that you need to change your hook like in the init() method to be this:

$this->addHook('PageArray::toJSON', $this, 'pagesToJSON');

I am guessing that the source if the error is that you are trying to call toJSON() on a PageArray when it's not hooked there. Hopefully the above change should fix it.

#8 evanmcd

evanmcd

    Full Member

  • Members
  • PipPipPip
  • 82 posts
  • 28

Posted 11 July 2012 - 09:41 PM

Yes, Ryan you nailed it. I fixed that up (and also another small fix to how I called the pageToArray function) and it's good to go.

Do you still prefer that we offer new modules up via a forum post, or is there another way?

Thanks for your as always excellent help :)

#9 ryan

ryan

    Hero Member

  • Administrators
  • 5,774 posts
  • 3122

  • LocationAtlanta, GA

Posted 12 July 2012 - 09:36 AM

Do you still prefer that we offer new modules up via a forum post, or is there another way?


Forum post for now, but that's short term. We'll be having a more official modules directory here soon. I plan to manually take everything from the forum to populate the modules directory initially.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users