Unserializing Wordpress data into JSON
-
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 http://wordpress.org/extend/plugins/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.
-
Answer:
PHP.js may help: http://phpjs.org/functions/unserialize/.
steinsaltz at Ask.Metafilter.Com Visit the source
Other answers
What does your query string look like?
djb
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.
steinsaltz
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.
djb
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.
steinsaltz
Thanks, the PHP.js script helped tons.
steinsaltz
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).
neckro23
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?
deathpanels
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.
steinsaltz
Related Q & A:
- How to set value in the dropdown from a JSON data list?Best solution by Stack Overflow
- How do I create an HTML table, in jQuery, with JSON data?Best solution by Stack Overflow
- How to store json data in jquery?Best solution by Stack Overflow
- How to read Json Data from online file?Best solution by mkyong.com
- How to deal with dynamic JSON data using AngularJS?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.