Join 3,433 readers in helping fund MetaFilter (Hide)


Unserializing Wordpress data into JSON
November 5, 2012 1:12 PM   Subscribe

I'm building a Javascript application that will do cool things with JSON data dumped from a client's Wordpress. It's working great except for parts that are in this PHP-style serialized data that I can't figure out how to deal with in Javascript. Can someone recommend a good method for this?

I was excited when I discovered the JSON API plugin for Wordpress, which lets WP produce data that is workable in Javascript applications like the one I am making.

Some of the data is easy to work with, like the "title" field. It shows up as a nice JSON string that I can work with. But many of the other Custom Fields are coming out as serialized data, like this:

"a:1:{s:39:"wpcf-fields-checkboxes-option-154377028";s:6:"medium";}"

I wonder if someone might be able to recommend either a good way of unserializing this in Javascript, or plugging something into the JSON API so that it filters and unserializes PHP before it gets dumped, or a better JSON API for Wordpress that does not do this?

I have been looking around on the Web and saw that there is a function called JSON.Parse that I was trying to use but couldn't figure out how to make it produce the right results or if it was what I should be using.

Any help much appreciated.
posted by steinsaltz to Computers & Internet (9 answers total) 1 user marked this as a favorite
 
PHP.js may help: JavaScript unserialize function.
posted by migurski at 1:42 PM on November 5, 2012 [1 favorite]


What does your query string look like?
posted by djb at 1:42 PM on November 5, 2012


djb, it's working from a static file right now, but I think the dump was generated by something like

http://www.domain.com/?json=

followed by some arguments for a certain Custom Post Type and to show 99 of them.
posted by steinsaltz at 1:47 PM on November 5, 2012


Did you define the custom fields manually or via a plugin? The snippet you posted suggests that the fields are serialized before being stored, which shouldn't happen. When I call the JSON API with arguments like http://example.com/?json=get_category_posts&slug=contacts&include=title,modified,custom_fields&custom_fields=phone&dev=1, the results are not serialized.
posted by djb at 1:57 PM on November 5, 2012


Oh, that's interesting. Yeah, the custom fields were developed, unfortunately, using a cumbersome plugin with unwelcome defaults. I think it may be the plugin that is doing the serializing.
posted by steinsaltz at 2:01 PM on November 5, 2012


Thanks, the PHP.js script helped tons.
posted by steinsaltz at 3:04 PM on November 5, 2012


If you're not averse to poking around in the plugin code, you should be able to replace the function serialize() with json_encode() where it occurs. This is assuming the code is only serializing on API output, of course (and not using the serialized data for some other nefarious purpose, like database storage).
posted by neckro23 at 6:31 PM on November 5, 2012


This doesn't help solve your problem, but this sounds like a bug in Wordpress's API to me. (Or a feature request waiting to happen.) Why the hell would the API not JSON-ify the serialized data for you?
posted by deathpanels at 10:00 PM on November 5, 2012


Thanks, nekro23. Unfortunately I suspect either Wordpress or this cheesy custom field plugin is committing the evil act of keeping serialized data in the MySQL. That stuff sure is cumbersome compared to other formats.
posted by steinsaltz at 11:32 PM on November 5, 2012


« Older Identify 2 silk plants please...   |  What watering hole should we w... Newer »
This thread is closed to new comments.