Serializing JSON Data

In this video, you’ll learn how to serialize Python objects into JSON.

The json module exposes two methods for serializing Python objects into JSON format.

dump() will write Python data to a file-like object. We use this when we want to serialize our Python data to an external JSON file.

dumps() will write Python data to a string in JSON format. This is useful if we want to use the JSON elsewhere in our program, or if we just want to print it to the console to check that it’s correct.

Python and JSON do not share all the same types. Serialization will convert your Python objects into JSON format according to this table

Python JSON
dict object
list, tuple array
str string
int, long, float number
True true
False false
None null

Both the dump() and dumps() methods allow us to specify an optional indent argument. This will change how many spaces is used for indentation, which can make our JSON easier to read.

json_str = json.dumps(data, indent=4)

00:00 Welcome back to our series on working with JSON data in Python.

00:05 In order to serialize data, we use two functions exposed by the json module: dump() and dumps(). dump() is used to write data to a file-like object.

00:17 We’ll use this within a with block in order to serialize native Python data into a JSON file. On the other hand, we have dumps(), which will serialize our data into a string in JSON format.

00:31 This is useful when we want to use the JSON-formatted data elsewhere in our program, or just preview it in the console without having to check our external JSON file. Python and JSON might rhyme, but they don’t use the same types. Lucky for us, most of the built-in types can easily be serialized into their JSON equivalents.

00:52 Python dictionaries are JSON objects, and lists and tuples are represented as arrays in JSON. The rest are pretty straightforward, although it should be noted that JSON clumps ints, longs, and floats into one category—call it number.

01:09 It also represents the Python NoneType as null.

01:14 Let’s take a look at how we serialize Python data to JSON format.

01:19 I’m here in Visual Studio Code in a blank Python file. We’re going to start by importing the json module, which will allow us to work with JSON data in our Python program.

01:30 We need some Python data to serialize, so we’ll create a new dictionary called data, and that will have a key value of "user". For the value we’ll create another dictionary, which will contain a "name" key with a value of "William Williams" and an "age" of 93.

01:52 Now that we’ve got our dictionary, we can serialize it. We’ll start by serializing the data into a separate JSON file. We’ll type with open("data_file.json") and we’ll open this with write access ("w") as the identifier write_file.

02:11 And once we’ve got that file, I’ll use the dump() function by writing json.dump(). Notice that this is dump() and not dumps(), because we’re writing to a file-like object. We’re going to supply two arguments, the first one being the data we want to serialize and the second being the tech stream we’re writing to. So now we’ve got our JSON in an external file, but I also want to print out a string representation of the JSON data. To do that, we’ll create a new string variable called json_str, and we’ll set it equal to json.dumps(). I’m using dumps() here because we’re writing this data to a string in memory, instead of a file.

02:54 And finally, I will print() this string to the console.

02:58 Now I will right-click and choose Run Code and immediately we’ll see that our JSON data has been printed to the console on the right. And if we look at the EXPLORER on the left of the screen, we’ll notice a new file was created called data_file.json.

03:15 If I click on that, we’ll see that our JSON file opens in the editor and it’s got the same content as we saw in the console. At this point, we could actually send this JSON file over a network.

03:27 If you notice, this looks a little bit cramped. It’s okay now, but if this file were a lot bigger or if we had more dictionaries within other dictionaries within other dictionaries, this would become pretty difficult for us to read. Remember, one of JSON’s strengths is that it’s readable by both machines and humans, so if we can’t read it, it’s difficult to work with. To fix this, let’s go back to our Python program and add another argument to the dump() function. This argument is called indent, and it will allow us to specify a number of spaces to use for each indentation.

04:03 I will set it equal to 4 spaces here and then I’ll also add this argument to the dumps() function call as well, since it works there too. And now if I right-click and run the program, we’ll see that our indentation rule has applied to both the console output on the right, and also—if I switch files here—the external data file with our JSON. At this point, we’ve seen how we can easily serialize a Python dictionary into JSON format.

04:31 Next, we’ll take a look at how we can deserialize some JSON data and use it within our Python program.

Become a Member to join the conversation.