dphiffer / wp-json-api

WordPress JSON API
http://wordpress.org/extend/plugins/json-api/
258 stars 181 forks source link

Modifing response format #39

Open andrewmclagan opened 7 years ago

andrewmclagan commented 7 years ago

Is there anyway to modify the response format from the standard?

The current standard is:

{
  "status": "ok",
  "count": 1,
  "count_total": 1,
  "pages": 1,
  "posts": [
    {
      "id": 1,
      "type": "post",
      "slug": "hello-world",
      "url": "http:\/\/localhost\/wordpress\/?p=1",
      "title": "Hello world!",
      "title_plain": "Hello world!",
      "content": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!<\/p>\n",
      "excerpt": "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!\n",
      "date": "2009-11-11 12:50:19",
      "modified": "2009-11-11 12:50:19",
      "categories": [],
      "tags": [],
      "author": {
        "id": 1,
        "slug": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "",
        "url": "",
        "description": ""
      },
      "comments": [
        {
          "id": 1,
          "name": "Mr WordPress",
          "url": "http:\/\/wordpress.org\/",
          "date": "2009-11-11 12:50:19",
          "content": "<p>Hi, this is a comment.<br \/>To delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.<\/p>\n",
          "parent": 0
        }
      ],
      "comment_count": 1,
      "comment_status": "open"
    }
  ]
}

We would like to use wp-json-api as a "headless CMS" integrated into our current API. Thus we would want to transform our responses as per the rest of our API:

{
  "data": {
    "posts": [
      {
        "id": 1,
        "title": "Hello world!",
        "title_plain": "Hello world!",
        "content": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!<\/p>\n",
        "categories": [92,61,21,6,32,7],
        "tags": [22,3,1,12],        
        "comments": [1], // see here
        "author": 1, // see here
      },
      {
        "id": 2,
        "title": "Another Post!",
        "title_plain": "Another Post!",
        "content": "<p>Another Post...<\/p>\n",
        "categories": [6,32,7],
        "tags": [17,1,44],        
        "comments": [], // see here
        "author": 1, // see here
      },      
    ],
    "comments": [
      {
        "id": 1,
        "name": "Mr WordPress",
        "url": "http:\/\/wordpress.org\/",
        "date": "2009-11-11 12:50:19",
        "content": "<p>Hi, this is a comment.<br \/>To delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.<\/p>\n",
        "parent": 0
      },
    ],  
    "authors": [
      {
        "id": 1,
        "slug": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "",
        "url": "",
        "description": ""
      }
    ], 
    "tags": [
      // ... you get the idea
    ],           
    "categories": [
      // ... you get the idea
    ],               
  }
}