1 00:00:00,320 --> 00:00:02,730 If you're going to build anything on the web, 2 00:00:02,730 --> 00:00:05,700 chances are you'll run into JSON data. 3 00:00:05,700 --> 00:00:10,640 You'll often receive a response in JSON when requesting data from an API. 4 00:00:10,640 --> 00:00:14,070 It's also a common format for configuration files. 5 00:00:14,070 --> 00:00:15,880 So what makes JSON so popular?. 6 00:00:16,885 --> 00:00:20,220 JSON is an easy way to encode structured data and 7 00:00:20,220 --> 00:00:22,930 transfer that data between systems. 8 00:00:22,930 --> 00:00:27,860 It has simple markup, and when formatted it's fairly easy for humans to read. 9 00:00:27,860 --> 00:00:30,170 It is also easily validated and 10 00:00:30,170 --> 00:00:33,310 transformed natively in most modern languages. 11 00:00:34,420 --> 00:00:37,860 JSON stands for JavaScript Object Notation, but 12 00:00:37,860 --> 00:00:39,930 we don't need to write any JavaScript. 13 00:00:39,930 --> 00:00:46,900 PHP has a handy function for turning JSON objects into PHP objects and arrays. 14 00:00:46,900 --> 00:00:52,910 We can also turn an object or array from PHP into JSON. 15 00:00:52,910 --> 00:00:56,330 Let's give it a try, while I share some of my top programming books. 16 00:00:56,330 --> 00:01:00,130 Open bookstop.,php, again, 17 00:01:00,130 --> 00:01:05,510 I've started by showing the contents of the JSON file and also a sample book. 18 00:01:05,510 --> 00:01:07,357 Let's preview this in a browser. 19 00:01:10,378 --> 00:01:16,440 We can see that the JSON file is clean and easy to read with a collection of books. 20 00:01:16,440 --> 00:01:19,840 These books have different attributes that are clearly defined. 21 00:01:21,110 --> 00:01:23,730 And here's what the sample book looks like. 22 00:01:23,730 --> 00:01:27,130 Let's go back to work spaces and remove the file display and 23 00:01:27,130 --> 00:01:30,015 replace the sample book with the data from the JSON. 24 00:01:36,173 --> 00:01:41,479 The first thing we need to do is get the contents of the JSON file. 25 00:01:56,041 --> 00:02:02,867 Then we'll use the json_decode function to turn this into an object. 26 00:02:05,512 --> 00:02:08,121 We'll assign this to the variable books. 27 00:02:10,438 --> 00:02:14,863 Before I try to use the object I wanna make sure that it's actually set up like I 28 00:02:14,863 --> 00:02:15,428 expect. 29 00:02:15,428 --> 00:02:21,267 If is_object books 30 00:02:21,267 --> 00:02:25,391 collection. 31 00:02:28,449 --> 00:02:29,829 And books. 32 00:02:31,892 --> 00:02:36,270 Should be an array of objects. 33 00:02:36,270 --> 00:02:38,483 Now I can loop through each book and 34 00:02:38,483 --> 00:02:41,777 replace the sample book with the book properties. 35 00:04:09,216 --> 00:04:11,230 Now, let's take a look at this in the browser. 36 00:04:17,925 --> 00:04:22,800 The details that I want are there but some of the descriptions are just too long. 37 00:04:22,800 --> 00:04:25,940 I don't want to display more than 500 characters. 38 00:04:25,940 --> 00:04:28,250 The book description itself is a string, so 39 00:04:28,250 --> 00:04:31,190 we can interact with it just like any other string. 40 00:04:31,190 --> 00:04:32,515 Let's go back to work spaces. 41 00:04:35,321 --> 00:04:40,044 First, I wanna check if the string is less than 500 characters. 42 00:04:58,435 --> 00:05:00,227 Then I'll just show the whole description. 43 00:05:02,536 --> 00:05:06,610 Else I'm going to use substring. 44 00:05:06,610 --> 00:05:13,686 I want to start at the beginning, echo substring 45 00:05:13,686 --> 00:05:19,829 book book description. 46 00:05:19,829 --> 00:05:23,822 To start at the beginning, I use 0. 47 00:05:23,822 --> 00:05:27,074 Then for the endpoint I could just use 500, but 48 00:05:27,074 --> 00:05:30,455 it might cut off in the middle of the word. 49 00:05:30,455 --> 00:05:33,275 Instead I'll use the string position and look for 50 00:05:33,275 --> 00:05:35,915 the next space after 500 characters. 51 00:05:55,938 --> 00:06:02,051 And finally all in the line with three dots. 52 00:06:02,051 --> 00:06:03,924 Let's space this out a little bit. 53 00:06:14,087 --> 00:06:16,083 Now let's go back to the browser. 54 00:06:22,195 --> 00:06:24,520 Great, the books are much more uniform.