JSON. JavaScript Object Notation. It's a small, lightweight, data format. A packet
of JSON data is almost identical to a Python dictionary. It is shorter than XML,
and can be quickly parsed by browsers since it uses JavaScript syntax. This makes
JSON an ideal format for transporting data between a client and a server. If
your client is not a browser, don't worry. Android and other mobile operating
systems all come equipped with tools for parsing and working with JSON. Today, I
will show you how to use Python's JSON library to send and receive JSON data
packets. We'll have a ton of fun with JSON sent from my Rayguns. Pew Pew!
Here is a typical JSON data packet. Notice how it looks just like a Python dictionary with
a few exceptions. True and false are not capitalized in JSON, while in Python
they are. And instead of none you use JavaScript value null. This JSON object
contains examples of all possible data types. All the keys are strings, but the
values can be strings, numbers, booleans, a list, null, or even another JSON object. Now
compare this with the XML version. The increased size of XML data is largely
due to the end tags repeating the text of the opening tags. A popular sport is
debating the merits of JSON versus XML. But instead of arguing, I recommend you
learn the pros and cons of both formats, then choose the one which is best for your project.
Let's now learn how to use the JSON module in Python.
First, let's save the sample JSON data to a txt file.
Notice the Cinematographer's name
contains a non ASCII character. For this reason, we will save this text file using
utf-8 encoding. Begin by importing the JSON module. Next, let's look at the
methods in this module. Today, we will focus on the load and dump methods.
The load method allows you to load JSON data from a file, while the load s method
enables you to load JSON data from a string. That's why there's an S at the
end of the method name: s for string. The dump method will
write JSON data to a file, and the dump s method will output a string in proper
JSON format. Let's see some examples. First, we will load the JSON data from
the file we created earlier. Using the open function, the first argument is the
path to the file. For the second argument, let's pass R, since we are only reading
the data. Next, specify the encoding is utf-8, since this data contains non ASCII
characters. Now that we have opened the file, we can load the JSON data using the
load method. After loading the JSON data, close the file. If you display the object,
you will see a dictionary containing all the data. If you look at the type, you
will see it is, in fact, a dictionary. Also notice how the true/false and null
values from the file were correctly parsed into Python true/false and none
values. And the non ASCII character was correctly parsed. If you look up the 142
Unicode character, it is a lowercase L with the stroke. Because this is a
dictionary, you can access the data by key. We can see the title, the list of
actors, and so on.
You use the load s function if your JSON data arrives in
the form of a string. This is common in client-server applications where data is
sent over the net. As an illustration, let's create a string with a JSON
formatted value. To parse this JSON data, call the load s function. Since all
characters are ASCII, there is no need to worry about encoding. If you look at the
result, we have a valid Python dictionary with all data properly converted. False
is the Python boolean and null was converted to none.
Suppose you want to store the data about Gattaca in a database, or send it to a remote user.
To convert this dictionary into a valid JSON string you use the dump s method
When you call the method simply pass in the dictionary. The result is a string in
proper JSON format. Notice that true and false are both lowercase, and none was
converted to null. There is an unusual double stroke for the non ASCII
character, however. This is because the JSON library assumes you want the output
to be ASCII, and will escape all non-ASCII
characters. To avoid this, and allow Unicode characters in your string, call
dump s, and set ensure ASCII to false. The non ASCII character is now preserved and
the backslash is not escaped.
Let's now create a new object, convert it to JSON,
and write it to a file. we start by creating a dictionary. For this example,
we will use data for the movie Minority Report. Directed by Steven Spielberg, with
a soundtrack by John Williams... this is a must-see movie for any Python programmer.
The cinematographer is Janusz Kaminsky. The N in his last name has an accent
with a Unicode values of 144. To write this to a file in JSON format, we must
first open a file in write mode with utf-8 encoding. Next call the dump
method. The first argument is the dictionary. Now pass in the file.
Remember to set ensure ASCII to false, since our data contains non ASCII characters.
Finally, close the file. If we open the file, we see all the data is there.
Everything is properly formatted, and the Unicode characters are all accounted for
Please wait for an incoming message from Socratica...
This JSON says Patreon helps Python to carry on...
and since rhyming sentences are twice as likely to be true
as ordinary ones, this message must be as good as gold...
which is what we are asking for, if you have some to spare.