2

why not WP_query?

http://www.htmlgoodies.com/beyond/javascript/provide-a-json-feed-from-your-wordpress-site-using-the-json-api-plugin.html, here if you look at the comment at the bottom the guy wants to display the recent posts using the JSON API plugin

but why bother, when you can do it with WP_query?

2

The JSON API is just a wrapper for WP_Query bundled with RESTful endpoints (add_rewrite_rules).

Its a way to speed up production by providing a framework which you otherwise would have to code yourself. In fact, if you build custom endpoints, you'll end up using your own QP_Query loops!

The choice is really going to come down to the requirements of your project.

If you are displaying the posts in your site, use WordPress' loop with your custom WP_Querys.

If you are planning on using it as a RESTful interface for other websites and apps, then the article you posted is spot on because the JSON API:

  • Provides stable endpoints (with versions) for other services to connect to
  • Is safe and time tested, as opposed to building your own API, as you can introduce vulnerabilites
  • Provides a way to find endpoints programatically (info)
  • Is headless, meaning it bypasses rendering any themes, making it faster

That being said, its not nescessarily the right solution. The best solution is always the one you can execute well.

Have a look at the WP REST API for more:

https://wordpress.org/plugins/json-rest-api/

Hope it helps!

2

There is no real reason at all, and the reasons are:

  1. you need to load more JS, either as more code, or even worse extra, JS files

  2. You need to do 2 requests to the sever instead of one. Since the performance of handling the JSON request is int the same order of the performance of handling a page, every page displayed will require about twice the resources that are neaded without JSON

  3. Unless you are going to do something trivial that exactly matches what the API provides, you are not going to save much coding. For example if you need some meta value to sort by, you will still need to find out how to add the data to the request, which is not significantly easier than writing the wp_query code

  4. It is much harder to debug JS code in the browsers than PHP code on the server

  5. JS is fragile and sometimes fail to load, or loads slowly. Failures can be caused by people that actively block JS (ludits that I personally ignore), but can also be caused by bad network, or the execution might never happen because of an earlier exception that comes from some other code.

Where it is useful is when you want to supply something like "recent posts" to other sites, but there is nothing new there that could not have been done with an iframe and wp_query (or oEmbed).

  • By 2 requests to the server you mean, the data should be encoded to JSON and then requested with the GET method then decoded? – john-thomas Feb 5 '17 at 13:23
  • no, I mean that first you need to request the HTML page, and only than you request the JSON – Mark Kaplun Feb 5 '17 at 13:40
0

JSON format is very compact and smart and it is the native object format for JavaScript.

If you check JavaScript objects they are using this format for declaring objects.

XML formats are little harder to parse, and this may be the reason to put them under the carpet.

Even if you check CSS, this is pretty damn close to JSON, the only difference is the in there you are using ; separator instead of ,.

CSV formats are cool but still not as cool as JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.