What is JSON?

If you’ve ever watched a YouTube tutorial or gone through a Udemy course on WordPress or Javascript, this acronym will definitely have popped up. However it is one of this terms that people just expect you to know what it is, and even though I have gone through hundreds of hours of web tutorials and videos, it was not till recently that I actually understood what it meant!!

JavaScript Object Notation

This is what JSON actually stands for – but what is it? To understand this you first need to appreciate what happens when you click on a link to a post, on a WordPress site for instance. The computer or phone you are browsing on sends a request to the server. The server looks up the post content you want and sends back a response. This will often (but not always ) be a JSON response. This just means that it is formatted as a Javascript object. This is a very useful way of packaging up information, and looks something like this.

var car = {type:"Fiat", model:"500", color:"white"};

Note the ‘key value’ pairs, this is how Javascript objects are constructed, but a page of JSON info wouldn’t look exactly like this…

{"generalInfo":[],"professors":[{"title":"Dr. Barksaloz","permalink":"http:\/\/localhost:10003\/professor\/dr-barksaloz\/","image":"http:\/\/localhost:10003\/wp-content\/uploads\/2021\/01\/barksalot-scaled.jpg"}],"programs":[{"title":"Biology","permalink":"http:\/\/localhost:10003\/programs\/biology\/","id":62}],"events":[{"title":"The Science of Cats","permalink":"http:\/\/localhost:10003\/events\/the-science-of-cats\/","month":"Nov","day":"13","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna…"}],"campuses":[]}

This is kind of incomprehensible to the human eye. But it is very efficient because there is not much wastage in the text, like whitespace. This is important, because one of the goals of an efficient code base is to keep server requests to a minimum and make them as small and fast as possible, which is a good reason to use JSON in the first place.

WordPress makes it easy

One of the great things about WordPress (from a developer’s point of view) is that when it see’s a JSON response, it automatically sorts it out into a more usable and readable format! This makes customising the WordPress api much easier.

There is more to learn about JSON, but this is a quick summary.