Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.3 JSON

In this lesson, I’ll teach you the basics of JSON as a way of interchanging data. I’ll introduce you to JSON syntax, its structures, and its data types. I’ll also compare JSON with XML and explain why JSON is used in REST APIs instead of XML.

Related Links

2.3 JSON

Hey folks, and welcome back to the course introducing the WP REST API. I'm an instructor, Bilal Shahid. In this lesson, we will take a brief overview of JSON, what is it and how it works. This is important because the WP REST API works heavily with the data in JSON format. So we need to be familiar with this method of representing data. JSON stands for JavaScript Object Notation and it's a way of representing data. If you have previously worked with JavaScript, then this notation would seem quite familiar as they're simply a subset of a JavaScript object. And it uses key value pairs with keys representing property names and values representing the actual property values. All right, so here we have a JSON object representing a single post and this object contains properties such as the ID of the post. Its title, its content, its author and its categories and text. These keys coming before : are the property names and anything coming after the : is the value of that property. And each key value pair is separated by a comma except the last one. And for the values of these properties, we have different data types including numbers, strings, arrays and Booleans. So the idea of the post is an integer as we can see in this demo object and it has a value of 10. The next property, that is the date property is a string. The strings are enclosed in double quotes and note that only double quotes are allowed in JSON. And we cannot enclose the strings or property names inside single quotes. So if I try to enclose any string inside single quotes, this will give me an error as displayed by the sublime text and text highlighting feature. So, I'll change them back to double quotes. So the values of all these properties are of diverse string. Next, we have this title property that contains the title of the post. But this this title property is not a string. Instead, it contains an object and inside that object it contains a property named rendered that contains the actual value of the title. And this is also the case with the content property. It also contains an object that in turn contains the random content that is also a valid HTML string enclosed in site . The author property contains the ID of the author who composed this post and this property is a number. The sticky property contains a Boolean value for whether or not this post is a sticky post. Currently, this is showing false. So, we can assume that this post is not a sticky post. The last two properties contain any values for the categories and text for this post. Arrays are enclosed by square brackets and the values inside them are separated by commas. The categories array list all the categories assigned to this post. So, here we can see that this post contains four categories whose IDs are 2, 4, 3 and 5. And there are three tags assigned to dispose the IDs of 6, 7, and 8. The current JSON file is holding an object for a single post. But if we need to display multiple posts, we can express them in an array, as we are doing in this JSON file. So, there are three post objects in this array with the IDs of 10, 11, and 12. And together, these objects and arrays formed the two structures on with JSON as built. So, whenever we are working with some JSON data, whether in the WPS REST API or using any other API, the interchange data will always be neither of these two formats. That is an object or an array. So, it is not possible that the server returns only a key value pair like this one. This is not a valid JSON data and we can validate it from using this JSON validator that is called JSONLint. And you can access it from its official website. So this must be enclosed in curly brackets to express an object or otherwise should be expressed in at a notation. Okay, so well, there are a number of other methods of presenting data of it XML being the most popular alternative to JSON. But the problem with XML is that it's quite heavy to process and it is not as concise and expressive as JSON. On the other hand, JSON is very lightweight and human readable form of representing data. And since it is based on JavaScript, it dominates the web development field. Let's view an example. This is the typical representation of a single post object in XML format. And it contains the main post note that in turn contains various child notes for representing different properties of that post. And this is the JSON representation of the exact same post object. And by looking at both of these formats, it's clear that JSON is more readable and is more lightweight and it also takes much less space for the same amount of data than the XML format. This is not to say that JSON is superior to XML but rather, it really depends on the use case and for the most REST APIs out there, JSON is the first choice due to its light weight and human readable form. All right, so this was a basic view over JSON, its structure, its data types and its advantages. In the next section, we will set up basic working environment to begin working with the WPS REST API. See you in the next video.

Back to the top