WordPress.org

Ready to get started?Download WordPress

Forums

JSON API
modify json output for easier parsing (3 posts)

  1. pboes
    Member
    Posted 7 months ago #

    Hi,
    I want to use the json api plugin together with the d3.js library to produce dynamic visualizations of my wordpress data. My problem is that the default root that the plugin's api produces means that I cannot simply call the resulting json back as in d3.json("url", function(error, root)), because parsing from the root will create problems.
    As I see it, there are two options: 1. Customize the output of the json file or 2. specify the parsing level of the json file (i.e. replace "root" in the above function)
    It seems to me that the second option would be simpler but in any case I don't know how to do either of the two.

    I'd really appreciate anybody's help on this.
    paul

    http://wordpress.org/plugins/json-api/

  2. pboes
    Member
    Posted 7 months ago #

    A specific example:
    query Querying "http://localhost/mysite/?json=get_page_index&include=title,type" gives

    {
    "status": "ok",
    "pages": [
    {
    "id": 42,
    "type": "page",
    "title": "Home",
    "children": []
    },
    {
    "id": 58,
    "type": "page",
    "title": "Edit Post",
    "children": []
    },
    {
    "id": 62,
    "type": "page",
    "title": "Dashboard",
    "children": []
    }

    etc...

    The file is supposed to work in this code:

    d3.json("json-url", function(error, root) {
    var nodes = tree.nodes(root),
    links = tree.links(nodes);

    var link = svg.selectAll(".link")
    .data(links)
    .enter().append("path")
    .attr("class", "link")
    .attr("d", diagonal);
    ....etc....
    node.append("text")
    .attr("dy", ".31em")
    .attr("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
    .attr("transform", function(d) { return d.x < 180 ? "translate(8)" : "rotate(180)translate(-8)"; })
    .text(function(d) { return d.title; });

    etc...
    So I would either have to get the plugin give me an output of the form

    {
    "title": "foo",
    "children": [
    {
    "id": 42,
    "type": "page",
    "title": "Home",
    "children": []
    },
    {
    "id": 58,
    "type": "page",
    "title": "Edit Post",
    "children": []
    },

    or get the d3.json-function to query not from the root...hope this helps

  3. Bob Gregor
    Member
    Posted 5 months ago #

    I'd recommend building your data structure by hand in PHP, then calling json_encode. I'd look @ the source of JSON API for a reference on how they get the starting structure that you're used to, then hack from there!

Reply

You must log in to post.

About this Plugin

About this Topic