Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
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.
The behavior of these constants is described on the JSON constants documentation. To read more about json_encode, see the documentation.
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.
-
0:00
The ease of working with JSON is one of the main reasons
-
0:03
it has become such a popular format for sharing data on the web.
-
0:07
We can take any object or array from PHP and turn it into a JSON
-
0:13
object that can then be shared without needing to rely on a particular language.
-
0:18
I can share the JSON for my recommendations and
-
0:21
they can be easily read using Python or any other language.
-
0:26
Communication is what has made the Internet such a powerful force.
-
0:30
Let's create another file in the inc directory.
-
0:33
We'll name this one
-
0:38
write_json [SOUND].
-
0:44
We'll start by creating an array for the new book.
-
0:47
[SOUND] This time,
-
0:56
we'll be using the FormData from the Add book form.
-
0:59
And our array will be an associative array.
-
1:04
Our title will be filter_
-
1:09
input(INPUT_POST, 'title, and
-
1:15
FILTER_SANITIZE_STRING..
-
1:21
We'll copy this for link, book, image, book description,
-
1:26
pages, author, ISBN, rating and publish date.
-
1:34
Link, [SOUND] and
-
1:39
this will be a URL.
-
1:46
Our book_image_url, [SOUND] which will also be a URL.
-
1:47
[SOUND]
-
1:49
Our book_description,
-
1:58
[SOUND] number
-
2:03
of pages [SOUND]
-
2:08
which will be
-
2:12
a NUMBER_INT.
-
2:18
[SOUND] author_name,
-
2:24
[SOUND] isbn [SOUND]
-
2:29
which will be
-
2:33
a NUMBER_INT.
-
2:38
[SOUND] Our
-
2:41
average_rating [SOUND]
-
2:49
which will be
-
2:53
a number float.
-
2:58
And finally book_published.
-
3:05
[SOUND] And this will
-
3:10
also be a NUMBER_INT.
-
3:17
Once again, we need to get the contents of the JSON file and
-
3:21
use the JSON decode function to turn it into an object.
-
3:25
We'll be using this file a couple of times.
-
3:27
So we'll set this up as a variable.
-
3:31
Our file = '
-
3:33
../data/json/top_programming_books.json.
-
3:45
books
-
3:46
=json_decode(file_get_contents
-
3:56
and the file.
-
4:02
Next, let's make sure the object is as expected.
-
4:09
Is_object($books->collection->books,
-
4:19
and the first array item.
-
4:31
Now we can add the new book to our book collection.
-
4:45
Next, we can use the json_encode function to turn our object back into JSON.
-
5:02
Before we actually write this back to our file,
-
5:04
let's take a quick look at the JSON itself, by displaying it to the browser.
-
5:11
Make sure we use the pretags.
-
5:21
And let's preview this in the browser.
-
5:24
We're going to add a book.
-
5:27
I'm going to pull the information from good reads.
-
6:07
There's no ISBN, so I'll leave that blank.
-
6:13
And the publish date was 2016.
-
6:15
I wanna pull the smaller image.
-
6:31
And let's add the book.
-
6:34
We have the json but it's not the prettiest.
-
6:37
Let's add a couple options to our json encode function.
-
6:41
See the notes associated with this video for more available options.
-
6:49
We'll use
-
6:51
JSON_PRETTY_PRINT |
-
6:57
JSON_UNESCAPED_SLASHES.
-
7:05
Now, let's preview this again.
-
7:09
Much better.
-
7:11
Let's write this back to our file and finish by redirecting to the books page.
-
7:18
Let's comment out the echo.
-
7:22
file_put_contents, file and our json.
-
7:33
Header is location: /books.php.
-
7:38
Let's check out the final functionality in the browser.
-
7:45
We fill out a form, submit it, and now our book shows up.
You need to sign up for Treehouse in order to download course files.
Sign up