Writing JSON7:52 with Alena Holligan
The ease of working with JSON is one of the main reasons it has become such a popular format for sharing data on the web. We can take any object or array from PHP and turn it into JSON that can then be shared without needing to rely on a particular language.
JSON Encoding Options
In this video, we show the use of JSON_PRETTY_PRINT and JSON_UNESCAPED_SLASHES. Other options that are available include: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR.
Validating & Formatting JSON
The JSON Formatter was created to help with debugging JSON. As JSON data is often output without line breaks to save space, it is extremely difficult to actually read and make sense of it. This little tool attemps to solve the problem by formatting the JSON data so that it is easy to read and debug by human beings.
The ease of working with JSON is one of the main reasons 0:00 it has become such a popular format for sharing data on the web. 0:03 We can take any object or array from PHP and turn it into a JSON 0:07 object that can then be shared without needing to rely on a particular language. 0:13 I can share the JSON for my recommendations and 0:18 they can be easily read using Python or any other language. 0:21 Communication is what has made the Internet such a powerful force. 0:26 Let's create another file in the inc directory. 0:30 We'll name this one 0:33 write_json [SOUND]. 0:38 We'll start by creating an array for the new book. 0:44 [SOUND] This time, 0:47 we'll be using the FormData from the Add book form. 0:56 And our array will be an associative array. 0:59 Our title will be filter_ 1:04 input(INPUT_POST, 'title, and 1:09 FILTER_SANITIZE_STRING.. 1:15 We'll copy this for link, book, image, book description, 1:21 pages, author, ISBN, rating and publish date. 1:26 Link, [SOUND] and 1:34 this will be a URL. 1:39 Our book_image_url, [SOUND] which will also be a URL. 1:46 [SOUND] 1:47 Our book_description, 1:49 [SOUND] number 1:58 of pages [SOUND] 2:03 which will be 2:08 a NUMBER_INT. 2:12 [SOUND] author_name, 2:18 [SOUND] isbn [SOUND] 2:24 which will be 2:29 a NUMBER_INT. 2:33 [SOUND] Our 2:38 average_rating [SOUND] 2:41 which will be 2:49 a number float. 2:53 And finally book_published. 2:58 [SOUND] And this will 3:05 also be a NUMBER_INT. 3:10 Once again, we need to get the contents of the JSON file and 3:17 use the JSON decode function to turn it into an object. 3:21 We'll be using this file a couple of times. 3:25 So we'll set this up as a variable. 3:27 Our file = ' 3:31 ../data/json/top_programming_books.json. 3:33 books 3:45 =json_decode(file_get_contents 3:46 and the file. 3:56 Next, let's make sure the object is as expected. 4:02 Is_object($books->collection->books, 4:09 and the first array item. 4:19 Now we can add the new book to our book collection. 4:31 Next, we can use the json_encode function to turn our object back into JSON. 4:45 Before we actually write this back to our file, 5:02 let's take a quick look at the JSON itself, by displaying it to the browser. 5:04 Make sure we use the pretags. 5:11 And let's preview this in the browser. 5:21 We're going to add a book. 5:24 I'm going to pull the information from good reads. 5:27 There's no ISBN, so I'll leave that blank. 6:07 And the publish date was 2016. 6:13 I wanna pull the smaller image. 6:15 And let's add the book. 6:31 We have the json but it's not the prettiest. 6:34 Let's add a couple options to our json encode function. 6:37 See the notes associated with this video for more available options. 6:41 We'll use 6:49 JSON_PRETTY_PRINT | 6:51 JSON_UNESCAPED_SLASHES. 6:57 Now, let's preview this again. 7:05 Much better. 7:09 Let's write this back to our file and finish by redirecting to the books page. 7:11 Let's comment out the echo. 7:18 file_put_contents, file and our json. 7:22 Header is location: /books.php. 7:33 Let's check out the final functionality in the browser. 7:38 We fill out a form, submit it, and now our book shows up. 7:45
You need to sign up for Treehouse in order to download course files.Sign up