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
JSON objects aren't restricted to containing a single payload of information and can encapsulate several objects by means of an array. In this video we look at how we can decode a list of JSON objects into an array of Codable conforming types.
Code Snippets
-
0:00
Let's continue to build on our example to make this a tiny bit more complicated and
-
0:04
a bit more realistic.
-
0:06
In the notes section of this video,
-
0:07
I've included a link to an updated sample of this JSON.
-
0:10
Paste that in, in place of this existing string literal.
-
0:14
So I've added a new key to the dictionary,
-
0:16
named candidates, that includes information about
-
0:19
other search results candidate that are not the best book listed above.
-
0:24
The difference here is that when you query the key candidates,
-
0:27
the value is an array, which then contains several dictionaries,
-
0:33
each of which represents a valid book and an author instance.
-
0:38
While this seems complicated, there's a lot going on here,
-
0:40
we have an array, nested dictionaries, and then further nested dictionaries.
-
0:44
We can actually model this in our type without much additional work.
-
0:48
So going back to the type, let's add a stored property, and
-
0:51
I'm in the search result type.
-
0:54
Let's add a stored property to define this.
-
0:56
Again, the key to properly doing this is understanding that our best bet
-
1:01
is matching the JSON structure as closely as possible.
-
1:05
We already know that if we have a nested dictionary containing book and author
-
1:09
data, the book type and then the author type can handle that decoding itself.
-
1:14
So we don't need to worry about that part.
-
1:17
The only new thing here is that the three dictionaries are inside an array, so
-
1:22
we need to model it as an array of books.
-
1:24
So here we'll say let candidates is in array of Book types, okay?
-
1:32
So again, back to CodingKeys, we need to add an enum member to the keys types.
-
1:37
So here, we'll say case candidates.
-
1:40
And then, in the init method, instead of asking the decoder to decode this into
-
1:45
a single instance of book, we'll specify an array of books.
-
1:50
So self.candidates = try innerContainer.decode,
-
1:58
array of Book.self, forKey.candidates.
-
2:05
If this seems weird, remember that an array of Book is a valid type.
-
2:09
So we're not doing anything wildly different here.
-
2:12
We can inspect this new property, so let's go down here.
-
2:16
And I'll say result.candidates.count.
-
2:21
And we should have a value of 3.
-
2:23
Look at that, done.
-
2:25
You'll often run into arrays containing dictionaries worth of information.
-
2:30
When you do, now you know that an implementation allowing for
-
2:33
the decoder to do much of the work, takes only a few lines of code.
-
2:38
Now a little bit of extra as to what's going on behind the scenes.
-
2:41
So when you call try innerContainer.decode Book.self,
-
2:46
what the decoder does is it creates an unkeyed container, right?
-
2:51
There are no keys in an array, just index values.
-
2:54
So it would be something like calling try
-
2:57
innerContainer.nestedUnkeyedContainer forKey.candidates.
-
3:03
And then, it would iterate through all of those, and
-
3:06
we'll look at how that happens in the future.
-
3:08
But it would iterate through all of those and for each key value pair or
-
3:12
rather no key value pairs.
-
3:13
But for each dictionary at each index value,
-
3:16
it's going to hand it off to the book type which will decode it and
-
3:20
come back eventually with an array result, okay?
-
3:23
What else?
-
3:24
So the encoding bit is also handled automatically for us.
-
3:28
Once we go in here, we just have to write one line of code.
-
3:31
We'll say try innerContainer.encode candidates, forKey: .candidates.
-
3:37
And again here, it's going to create a nested unkeyed container.
-
3:41
And if you look at our JSON string down here in the console, the dictionaries.
-
3:46
So we have the candidates key right at the top here, and
-
3:48
you'll notice that there's an array at the top, and then each value is a dictionary.
-
3:54
You can encounter information encoded in a combination of dictionaries and
-
3:58
arrays in many ways.
-
4:00
But as I mentioned earlier, the important part is modeling it just as the JSON is.
-
4:05
And you shouldn't have much difficulty parsing it.
-
4:08
For example, let's go back up to the top here, and we'll extract this candidates.
-
4:13
Where are we are?
-
4:15
Okay, so, over here, grab everything from the candidates key,
-
4:18
the key-value pair, all the way to the bottom where the array ends.
-
4:23
And then at the very end, we'll just put this in its own JSON string here.
-
4:28
So let candidatesJson =, remember
-
4:33
multi-line string literal, paste it inside.
-
4:39
We don't need to format this properly, and
-
4:42
we'll say .data using utf8 string encoding, okay.
-
4:47
Now if we wanted to parse this blob of JSON into an array of books,
-
4:51
without worrying about nested keyed containers and unkeyed containers.
-
4:55
Again, the way here, so notice here that we sort of have a wrapper key, right?
-
5:01
So the information is an array of books, which is the value.
-
5:04
And then we have a key to get to that, and if we wanted to decode this directly,
-
5:09
again a combination of a dictionary and an array of dictionaries,
-
5:12
we would say let candidates = try decoder.decode.
-
5:19
Again, remember is to modeling it exactly like the JSON is,
-
5:23
which is a dictionary with a key as a String.
-
5:26
And the value is an array of books.
-
5:30
And then on that entire type, we'll call .self, using candidatesJson.
-
5:38
I made a typo.
-
5:40
Let's see.
-
5:40
This is not using, I always get this key wrong, it's from.
-
5:44
Okay, and when we do that, what did I do wrong?
-
5:51
I've not copy pasted this JSON directly.
-
5:53
I'm pretty sure, okay, Enter, and I missed a closing dictionary brace, or did I?
-
6:01
And I missed the opening, right.
-
6:04
You need to start with an opening brace for a dictionary, then a key.
-
6:08
Okay, and when you do that, you'll see here that we have a dictionary
-
6:12
that has a key candidates and then an array of books in there.
-
6:16
The type here is string to an array of books .self.
-
6:21
So it's important to get that part right.
-
6:24
Resemble the JSON as much as possible.
-
6:26
Okay, let's wrap it up here.
-
6:28
In the next video, we'll look at what happens when data is potentially missing.
You need to sign up for Treehouse in order to download course files.
Sign up